Difference between revisions of "Node.js"

From ArchWiki
Jump to navigation Jump to search
(indicate root terminal by using # over $)
(Clarifying that a custom nvm directory is basically mandatory, unless running as root.)
 
(46 intermediate revisions by 29 users not shown)
Line 1: Line 1:
 
[[Category:Development]]
 
[[Category:Development]]
[[zh-CN:Node.js]]
+
[[ja:Node.js]]
[http://nodejs.org/ Node.js] is a javascript runtime environment combined with useful libraries.
+
[[zh-hans:Node.js]]
It uses [https://code.google.com/p/v8/ Google's V8 engine] to execute code outside of the browser.
+
[http://nodejs.org/ Node.js] is a JavaScript runtime environment combined with useful libraries. It uses [https://v8.dev Google's V8 engine] to execute code outside of the browser. Due to its event-driven, non-blocking I/O model, it is suitable for real-time web applications.
Due to its event-driven, non-blocking I/O model, it is suitable for real-time web applications.
 
  
 
== Installation ==
 
== Installation ==
  
{{ Pkg | nodejs }} package is available in the [[ official repositories ]].
+
[[Install]] the {{Pkg|nodejs}} package.
 +
 
 +
=== Alternate installations ===
 +
 
 +
It is not uncommon to need or desire to work in different versions of {{Pkg|nodejs}}. A preferred method among node users is to use [https://github.com/creationix/nvm NVM] (Node Version Manager). {{AUR|nvm}} allows for cheap and easy alternative installs.
 +
 
 +
You can set it up by adding this to your shell's startup file:
 +
# Set up Node Version Manager
 +
source /usr/share/nvm/init-nvm.sh
 +
 
 +
Keep in mind that by default, if you're not running as {{ic|root}}, you'll run into issues with pretty much any {{ic|nvm}} command as it will be trying to install Node versions and packages on system-level directories. This is not default behavior if you install {{ic|nvm}} via the script provided by the developers, where Node is kept in {{ic|$HOME/.nvm}}.
 +
 
 +
To emulate this default behavior, you can specify a custom nvm directory:
 +
 
 +
# Set up Node Version Manager
 +
export NVM_DIR="$HOME/.nvm"                            # You can change this if you want.
 +
export NVM_SOURCE="/usr/share/nvm"                    # The AUR package installs it to here.
 +
[ -s "$NVM_SOURCE/nvm.sh" ] && . "$NVM_SOURCE/nvm.sh"  # Load NVM
 +
 
 +
Usage is well documented on the project's GitHub but is as simple as:
 +
 
 +
$ nvm install 8.0
 +
Downloading and installing node v8.0.0...
 +
[..]
 +
 +
$ nvm use 8.0
 +
Now using node v8.0.0 (npm v5.0.0)
 +
 
 +
If you decide to use {{AUR|nvm}}, previously it was suggested to use {{ic|nodejs-fake}} package from AUR. Which is now [https://lists.archlinux.org/pipermail/aur-requests/2018-January/021828.html deleted].
 +
Suggested way is to use {{ic|1=--assume-installed nodejs=<version>}}, as per [https://www.archlinux.org/pacman/pacman.8.html#_transaction_options_apply_to_em_s_em_em_r_em_and_em_u_em pacman] manual.
  
 
== Node Packaged Modules ==
 
== Node Packaged Modules ==
  
[https://www.npmjs.org/ npm] is the official package manager for node.js which is included in {{ Pkg | nodejs }} package.
+
[https://www.npmjs.org/ npm] is the official package manager for node.js. It can be [[install]]ed with the {{Pkg|npm}} package.
  
 
=== Managing packages with npm ===
 
=== Managing packages with npm ===
Line 21: Line 49:
 
  $ npm install packageName
 
  $ npm install packageName
  
This command installs the package in the current directory under {{ ic | node_modules }} and executables under {{ ic | node_modules/.bin }}.
+
This command installs the package in the current directory under {{ic|node_modules}} and executables under {{ic|node_modules/.bin}}.
  
For a system-wide installation global switch {{ ic | -g }} can be used:
+
For a system-wide installation global switch {{ic|-g}} can be used:
  
 
  # npm -g install packageName
 
  # npm -g install packageName
  
By default this command installs the package under {{ ic | /usr/lib/node_modules/npm }} and requires admin privileges to do so.
+
By default this command installs the package under {{ic|/usr/lib/node_modules/npm}} and requires root privileges to do so.
 +
 
 +
===== Allow user-wide installations =====
  
For a user-wide installation you can configure {{ ic | npm }} to use a local folder instead.
+
To allow ''global'' package installations for the current [[user]], set the {{ic|npm_config_prefix}} [[Environment_variables#Per_user|environment variable]]. This is used by both npm and [https://yarnpkg.com/en/ yarn].
This can be done in various ways:
 
  
* Manually with the {{ ic | --prefix }} command line flag (e.g. {{ ic | npm -g install packageName --prefix ~/.node_modules }}).
+
{{hc|~/.profile|2=
* Using the {{ ic | npm_config_prefix }} environment variable.
+
PATH="$HOME/.node_modules/bin:$PATH"
* Using a user config file {{ ic | ~/.npmrc }}.
+
export npm_config_prefix=~/.node_modules
 +
}}
  
First method is not recommended since you need to remember the location and give it as the parameter each time you do an operation.
+
Re-login or ''source'' to update changes.
  
For the second method simply add the following lines to your shell configuration file (e.g. {{ ic | .bash_profile }}).
+
You can also specify the {{ic|--prefix}} parameter for {{ic|npm install}}. However, this is '''not''' recommended, since you'll need to add it every time you install a global package.
  
  PATH=$PATH:~/.node_modules/bin
+
  $ npm -g install packageName --prefix ~/.node_modules
export npm_config_prefix=~/.node_modules
 
  
Do not forget to log out and log back in or restart your shell accordingly.
+
==== Updating packages ====
  
For the third method you can use the command:
+
Updating packages is as simple as
  
  $ npm config edit
+
  $ npm update packageName
  
You can then find the {{ ic | prefix }} option and set a desired location:
+
For the case of globally installed packages ({{ic|-g}})
  
  prefix=~/.node_modules
+
  # npm update -g packageName
  
Do not forget to delete the preceding {{ ic|; }} on the line or it will be read as a comment.
+
{{Note|Remember that globally installed packages require administrator privileges unless {{ic|prefix}} is set to a user-writable directory}}
You can now add the location of your executables to your shell configuration file (e.g. {{ ic | .bash_profile }}).
 
  
PATH=$PATH:~/.node_modules/bin
+
===== Updating all packages =====
  
Again do not forget to log out and log back in or restart your shell accordingly.
+
However, sometimes you may just wish to update all packages, either locally or globally. Leaving off the packageName {{ic|npm}} will attempt to update all packages
  
==== Updating packages ====
+
$ npm update
  
Updating packages is as simple as
+
or add the {{ic|-g}} flag to update globally installed packages
  
  $ npm update packageName
+
# npm update -g
  
For the case of globally installed packages ( {{ic | -g}} )
+
==== Removing packages ====
  
  # npm update -g packageName
+
To remove a package installed with the {{ic|-g}} switch simply use:
 +
 
 +
# npm -g uninstall packageName
  
 
{{Note|Remember that globally installed packages require administrator privileges}}
 
{{Note|Remember that globally installed packages require administrator privileges}}
  
===== Updating All Packages =====
+
to remove a local package drop the switch and run:
 +
 
 +
$ npm uninstall packageName
 +
 
 +
==== Listing packages ====
 +
 
 +
To show a tree view of the installed globally packages use:
  
However, sometimes you may just wish to update all packages. Be it locally or globally. Leaving off the packageName {{ ic | npm }} will attempt to update all the packages
+
$ npm -g list
  
  $ npm update
+
This tree is often quite deep. To only display the top level packages use:
  
or add the {{ic | -g}} flag to update globally installed packages
+
$ npm list --depth=0
  
  # npm update -g
+
To display obsolete packages that may need to be updated:
  
==== Removing packages ====
+
$ npm outdated
  
To remove a package installed with the {{ ic | -g }} switch simply use:
+
=== Managing packages with pacman ===
  
# npm -g uninstall packageName
+
Some node.js packages can be found in [[Arch User Repository]] with the name {{ic|nodejs-packageName}}.
  
{{Note|Remember that globally installed packages require administrator privileges}}
+
== Troubleshooting ==
  
to remove a local package drop the switch and run:
+
=== node-gyp python errors ===
  
  $ npm uninstall packageName
+
Some node modules use the {{ic|node-gyp}} utility which does not support Python 3, which in most cases will be the default system-wide Python executable. To resolve such errors, make sure you have {{Pkg|python2}} installed, then set the default npm Python like so:
  
 +
$ npm config set python /usr/bin/python2
  
==== Listing packages ====
+
In case of errors like {{ic|gyp WARN EACCES user "root" does not have permission to access the ... dir}}, {{ic|--unsafe-perm}} option might help:
  
To show a tree view of the installed packages use:
+
# npm install --unsafe-perm -g node-inspector
  
# npm -g list
+
=== Cannot find module ... errors ===
  
=== Managing packages with pacman ===
+
Since npm 5.x.x. package-lock.json file is generated along with the package.json file. Conflictions may arise when the two files refer to different package versions. A temporary method to solving this problem has been:
 +
$ rm package-lock.json
 +
$ npm install
  
Some node.js packages can be found in [[ Arch User Repository ]] with the name {{ ic | nodejs-packageName }}.
+
However, fixes were made after npm 5.1.0 or above. For further information, see:
 +
[https://github.com/npm/npm/pull/17508 missing dependencies]
  
== Additional Resources ==
+
== Additional resources ==
  
For further information on {{ Pkg | nodejs }} and use of its official package manager [https://www.npmjs.org/ npm] you may wish to consult the following external resources
+
For further information on Node.js and the use of its official package manager NPM you may wish to consult the following external resources
  
* [http://nodejs.org/documentation/ NodeJs Documentation] Documentation and Tutorials for Node
+
* [https://nodejs.org/en/docs/ Node.js documentation]
* [http://nodejs.org/community/ NodeJS Community] Webpage
+
* [https://docs.npmjs.com/ NPM documentation]
* [https://www.npmjs.org/doc/ API Documentation] Official {{ ic | npm }} API documentation
 
 
* IRC channel #node.js on irc.freenode.net
 
* IRC channel #node.js on irc.freenode.net

Latest revision as of 11:25, 9 May 2019

Node.js is a JavaScript runtime environment combined with useful libraries. It uses Google's V8 engine to execute code outside of the browser. Due to its event-driven, non-blocking I/O model, it is suitable for real-time web applications.

Installation

Install the nodejs package.

Alternate installations

It is not uncommon to need or desire to work in different versions of nodejs. A preferred method among node users is to use NVM (Node Version Manager). nvmAUR allows for cheap and easy alternative installs.

You can set it up by adding this to your shell's startup file:

# Set up Node Version Manager
source /usr/share/nvm/init-nvm.sh

Keep in mind that by default, if you're not running as root, you'll run into issues with pretty much any nvm command as it will be trying to install Node versions and packages on system-level directories. This is not default behavior if you install nvm via the script provided by the developers, where Node is kept in $HOME/.nvm.

To emulate this default behavior, you can specify a custom nvm directory:

# Set up Node Version Manager
export NVM_DIR="$HOME/.nvm"                            # You can change this if you want.
export NVM_SOURCE="/usr/share/nvm"                     # The AUR package installs it to here.
[ -s "$NVM_SOURCE/nvm.sh" ] && . "$NVM_SOURCE/nvm.sh"  # Load NVM

Usage is well documented on the project's GitHub but is as simple as:

$ nvm install 8.0
Downloading and installing node v8.0.0...
[..]

$ nvm use 8.0
Now using node v8.0.0 (npm v5.0.0)

If you decide to use nvmAUR, previously it was suggested to use nodejs-fake package from AUR. Which is now deleted. Suggested way is to use --assume-installed nodejs=<version>, as per pacman manual.

Node Packaged Modules

npm is the official package manager for node.js. It can be installed with the npm package.

Managing packages with npm

Installing packages

Any package can be installed using:

$ npm install packageName

This command installs the package in the current directory under node_modules and executables under node_modules/.bin.

For a system-wide installation global switch -g can be used:

# npm -g install packageName

By default this command installs the package under /usr/lib/node_modules/npm and requires root privileges to do so.

Allow user-wide installations

To allow global package installations for the current user, set the npm_config_prefix environment variable. This is used by both npm and yarn.

~/.profile
PATH="$HOME/.node_modules/bin:$PATH"
export npm_config_prefix=~/.node_modules

Re-login or source to update changes.

You can also specify the --prefix parameter for npm install. However, this is not recommended, since you'll need to add it every time you install a global package.

$ npm -g install packageName --prefix ~/.node_modules

Updating packages

Updating packages is as simple as

$ npm update packageName

For the case of globally installed packages (-g)

# npm update -g packageName
Note: Remember that globally installed packages require administrator privileges unless prefix is set to a user-writable directory
Updating all packages

However, sometimes you may just wish to update all packages, either locally or globally. Leaving off the packageName npm will attempt to update all packages

$ npm update

or add the -g flag to update globally installed packages

# npm update -g

Removing packages

To remove a package installed with the -g switch simply use:

# npm -g uninstall packageName
Note: Remember that globally installed packages require administrator privileges

to remove a local package drop the switch and run:

$ npm uninstall packageName

Listing packages

To show a tree view of the installed globally packages use:

$ npm -g list

This tree is often quite deep. To only display the top level packages use:

$ npm list --depth=0

To display obsolete packages that may need to be updated:

$ npm outdated

Managing packages with pacman

Some node.js packages can be found in Arch User Repository with the name nodejs-packageName.

Troubleshooting

node-gyp python errors

Some node modules use the node-gyp utility which does not support Python 3, which in most cases will be the default system-wide Python executable. To resolve such errors, make sure you have python2 installed, then set the default npm Python like so:

$ npm config set python /usr/bin/python2

In case of errors like gyp WARN EACCES user "root" does not have permission to access the ... dir, --unsafe-perm option might help:

# npm install --unsafe-perm -g node-inspector

Cannot find module ... errors

Since npm 5.x.x. package-lock.json file is generated along with the package.json file. Conflictions may arise when the two files refer to different package versions. A temporary method to solving this problem has been:

$ rm package-lock.json
$ npm install

However, fixes were made after npm 5.1.0 or above. For further information, see: missing dependencies

Additional resources

For further information on Node.js and the use of its official package manager NPM you may wish to consult the following external resources