NodeJS: Difference between revisions

From David's Wiki
No edit summary
No edit summary
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.
Node Package Manager (npm) is used to manager node packages installed on your computer or used in your node projects.
Node Package Manager (npm) is used to manage node packages installed on your computer or used in your node projects.<br>
An alternative to npm is yarn.


==Installation==
==Installation==
Line 25: Line 24:
<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 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>.
<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==
==Useful Packages==
===fs-extra===
===fs-extra===
[https://www.npmjs.com/package/fs-extra fs-extra] adds additional commands for accessing the filesystem such as copying and moving files.
[https://www.npmjs.com/package/fs-extra fs-extra] adds additional commands for accessing the filesystem such as copying and moving files.

Revision as of 12:27, 26 August 2019

Node.js is a JavaScript environment based on Chrome's V8 JavaScript engine. Node Package Manager (npm) is used to manage node packages installed on your computer or used in your node projects.

Installation

Windows

Just download the latest LTS release from the Node.js website

Linux

Using the 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.

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/master/install.sh | bash
# Reload .bashrc
source ~/.bashrc
nvm install --lts node
nvm install-latest-npm

Usage

JavaScript files can be run directly using node index.js

NPM

To add node packages to an new project. Use npm init
To download the dependencies for a node project, run npm install or npm ci.
npm install installs the latest dependencies listed in package.json subject to the requirements listed in that file. It also updates package-lock.json.
npm ci installs exact versions from the package-lock.json.

Yarn

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

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

fs-extra adds additional commands for accessing the filesystem such as copying and moving files.