NodeJS: Difference between revisions

298 bytes added ,  25 September 2019
no edit summary
No edit summary
Line 2: Line 2:
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>


==Installation==
=Installation=
===Windows===
==Windows==
Just download the latest LTS release from [https://nodejs.org/en/ the Node.js website]
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. This does not require sudo as nvm is installed in your home directory.
You should never use sudo when using npm. Doing so could lead to permission issues.
You should never use sudo when using npm. Doing so could lead to permission issues.
Line 17: Line 17:
</syntaxhighlight>
</syntaxhighlight>


==Usage==
=Usage=
JavaScript files can be run directly using <code>node index.js</code>
JavaScript files can be run directly using <code>node index.js</code>
===NPM===
==Filesystem Access==
To add node packages to an new project. Use <code>npm init</code><br>
Install [https://www.npmjs.com/package/fs-extra fs-extra] to your project.
To download the dependencies for a node project, run <code>npm install</code> or <code>npm ci</code>.<br>
<code>npm install</code> installs the latest dependencies listed in <code>package.json</code> subject to the requirements listed in that file. It also updates <code>package-lock.json</code>.<br>
<code>npm ci</code> installs exact versions from the <code>package-lock.json</code>.
===Yarn===
[https://yarnpkg.com/en/ Yarn] is an alternative to npm. It caches packages on disk to speed up installs.
While you can migrate from npm to yarn, it is recommended to stick to a single package manager since they each use their own lockfile.
===pnpm===
[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.
 
==Useful Packages==
===fs-extra===
[https://www.npmjs.com/package/fs-extra fs-extra] adds additional commands for accessing the filesystem such as copying and moving files.
<syntaxhighlight lang="js">
<syntaxhighlight lang="js">
const fs = require('fs-extra');
const fs = require('fs-extra');
Line 67: Line 54:
fs.readFileSync('temp.txt', 'utf8');
fs.readFileSync('temp.txt', 'utf8');
</syntaxhighlight>
</syntaxhighlight>
==Logging==
Mostly the same as web JavaScript.<br>
[https://nodejs.org/api/console.html Reference]
<syntaxhighlight>
console.log("a", "b");
console.error("to stderr");
</syntaxhighlight>
=Package Management=
===NPM===
To add node packages to an new project. Use <code>npm init</code><br>
To download the dependencies for a node project, run <code>npm install</code> or <code>npm ci</code>.<br>
<code>npm install</code> installs the latest dependencies listed in <code>package.json</code> subject to the requirements listed in that file. It also updates <code>package-lock.json</code>.<br>
<code>npm ci</code> installs exact versions from the <code>package-lock.json</code>.
===Yarn===
[https://yarnpkg.com/en/ Yarn] is an alternative to npm. It caches packages on disk to speed up installs.
While you can migrate from npm to yarn, it is recommended to stick to a single package manager since they each use their own lockfile.
===pnpm===
[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.
=Useful Packages=
===fs-extra===
[https://www.npmjs.com/package/fs-extra fs-extra] adds additional commands for accessing the filesystem such as copying and moving files.