NodeJS: Difference between revisions

 
(14 intermediate revisions by the same user not shown)
Line 1: Line 1:
Node.js is a JavaScript environment based on Chrome's V8 JavaScript engine.
Node.js is a JavaScript environment based on Chrome's V8 JavaScript engine.<br>
Node Package Manager (npm) is used to manage node packages installed on your computer or used in your node projects.<br>
Node Package Manager (npm) is used to manage node packages installed on your computer or used in your node projects.<br>
This page is primarilly on node-specific APIs and usage. See also [[JavaScript]] and [[TypeScript]].


==Installation==
==Installation==
===Windows===
Just download the latest LTS release from [https://nodejs.org/en/ the Node.js website]
===Linux===
===Linux===
Using the [https://github.com/nvm-sh/nvm Node Version Manager (nvm)] is recommended. This does not require sudo as nvm is installed in your home directory.
Using the [https://github.com/nvm-sh/nvm Node Version Manager (nvm)] is recommended.<br>
You should never use sudo when using npm. Doing so could lead to permission issues.
This installs nvm in your home directory.
You should never use sudo when using nvm or npm.  
Doing so could lead to permission issues.
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/master/install.sh | bash
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/master/install.sh | bash
# Reload .bashrc
source ~/.bashrc
source ~/.bashrc
nvm install --lts node
nvm install --lts node
nvm install-latest-npm
nvm install-latest-npm
</syntaxhighlight>
===Windows===
You can [https://nodejs.org/en/ install node directly] or use [https://github.com/coreybutler/nvm-windows nvm-windows].<br>
Note that nvm-windows is not a port of nvm.
<syntaxhighlight lang="bash">
choco install nvm
nvm install lts
</syntaxhighlight>
</syntaxhighlight>


Line 79: Line 86:
<syntaxhighlight lang="js">
<syntaxhighlight lang="js">
</syntaxhighlight>
</syntaxhighlight>


===Running other processes===
===Running other processes===
You can invoke other processes or CLI programs using <code>spawn</code> or <code>spawnSync</code>
You can invoke other processes or CLI programs using <code>spawn</code> or <code>spawnSync</code>
<syntaxhighlight lang="javascript">
<syntaxhighlight lang="javascript">
const {spawn} = require('child_process');
const {spawn, spawnSync} = require('child_process');
const {spawnSync} = require('child_process');
</syntaxhighlight>
</syntaxhighlight>


Line 101: Line 106:
});
});
</syntaxhighlight>
</syntaxhighlight>
==Debugging==
===Inspector===
* Add the <code>--inspect</code> flag to your node call (before the name of the script).
* Then go to <code>chrome://inspect</code>


==Package Management==
==Package Management==
Line 114: Line 124:
[https://pnpm.js.org pnpm] uses symlinks to a single copy of each dependency to save disk space.
[https://pnpm.js.org pnpm] uses symlinks to a single copy of each dependency to save disk space.
pnpm uses its own lockfile instead of packages-lock.json.
pnpm uses its own lockfile instead of packages-lock.json.
===Publishing===
See [[Npm (software)#Publishing]].


==Useful Packages==
==Useful Packages==
Line 120: Line 133:
===node-fetch===
===node-fetch===
[https://www.npmjs.com/package/node-fetch node-fetch]<br>
[https://www.npmjs.com/package/node-fetch node-fetch]<br>
===file-type===
[https://www.npmjs.com/package/file-type file-type]<br>
[https://www.npmjs.com/package/file-type-cli file-type-cli]<br>
Detects the file type of a file or buffer
===argparse===
[https://www.npmjs.com/package/argparse argparse]
This is a port of python's argparse. However, it does not support configuration files.
{{hidden | Example |
<syntaxhighlight lang="python">
const parser = new ArgumentParser({
  version: "0.0.1",
  addHelp: true,
  description: "Description here"
});
// Add your arguments here
parser.addArgument(["--config"], {
  type: String,
  help: "Path to config file (optional)"
});
const args = (function() {
  let myArgs = parser.parseArgs();
  if (myArgs.config && myArgs.config.length > 0) {
    const filedata = fs.readFileSync(myArgs.config, "utf8");
    const myArgsArr = filedata
      .split("\n")
      .filter(x => x.length > 0)
      .map(x => "--" + x)
      .map(x => x.split("="))
      .flatten();
    myArgs = parser.parseArgs(myArgsArr);
  }
  return myArgs;
})();
</syntaxhighlight>
}}
===node-worker-threads-pool===
[https://www.npmjs.com/package/node-worker-threads-pool Link] 
A cool thread pool implementation.