Node.js
Node.js® is a JavaScript runtime built on Chrome’s V8 JavaScript engine. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient. Node.js’ package ecosystem, npm, is the largest ecosystem of open source libraries in the world.
node
Without argument it launches a JavaScript REPL
npm
The Node Package Manager npm can install packages in local or global mode.
In local mode it installs the package to the folder node_modules
in the current working directory.
in global mode packages are installed to /usr/lib/node_modules/
which needs sudo
.
Changing Location of Global Packages
Change the prefix to a folder in the HOME dir:
cd ~ && mkdir .node_modules_global
npm config set prefix=$HOME/.node_modules_global
$ cat .npmrc
# prefix=/home/andre/.node_modules_global
Then reinstall npm and add new global bin dir to $PATH:
npm install npm --global
# add .bashrc
export PATH="$HOME/.node_modules_global/bin:$PATH"
which npm
# /home/sitepoint/.node_modules_global/bin/npm
Installing Packages in Global Mode
The Flag --global
or just -g
is used.
npm install uglify-js -g
Listing Global Packages
The installed packages with a full dependency graph are shown with
npm list -g
In order to see just packages we do:
npm list -g --depth=0
Installing Packages in Local Mode
For local installation a package.json
file is used most of the time:
npm init
creates the file:
{
"name": "project",
"version": "1.0.0",
"description": "",
"main": "index.js"
}
It is modified when a new dependency is installed:
npm install underscore
{
"dependencies": {
"underscore": "^1.8.3"
}
}