DevOps Tools/Repository Management/npm
npm
Contents |
Overview
npm is the Node Package Manager. It provides access to a whole ecosystem of Node-based tools for developers to use when creating a server environment for applications or simply to aid them in local task automation. It is also commonly used with CI tools like Jenkins to automate build, test, prod, and other phases.
Installation
https://github.com/nodesource/distributions/blob/master/README.md
Ubuntu
~$ curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash - ~$ sudo apt-get install -y nodejs
CentOS
~$ sudo curl -sL https://rpm.nodesource.com/setup_12.x | bash -
Verify Installation
~$ which node /usr/bin/node ~$ node --version v12.11.1 ~$ which npm /usr/bin/npm ~$ npm --version 6.11.3
Make sure Node is working properly.
~$ node Welcome to Node.js v12.11.1. Type ".help" for more information. > console.log('Node is running') Node is running undefined > .exit
Update
CentOS
~$ npm install npm@latest -g
Node Modules
Local vs Global
- Local
- installs the package in a
node_modules
in your parent working directory. In general, all packages should be installed locally. This allows you to have dozens of the same applications on your computer, all running a different version of each package if needed. - Global
- installs the package in
{prefix}/lib/node_modules
which is owned by root (where{prefix}
is usually/usr/ or /usr/local/
. A package should be installed globally when it provides an executable command that you run from the shell and is reused across projects.
NOTE: regarding global packages, you can use npm config
to change the directory prefix to somewhere not owned by root, such as $HOME
, however in most modern deployments you'll be using npm in an ephemeral container where the user is already root. It should also be noted that most packages that are installed globally, like CLI, are used by the developer in a manual fashion rather than in an automated deployment.
Package Installation
Global
Global packages are installed with the --global
flag.
~$ npm install uglify-js --global
You can list them using:
$ npm list --global home/user/.node_modules_global/lib ├─┬ [email protected] │ ├── [email protected] │ ├── [email protected] │ ├── [email protected] │ ├── [email protected] │ ├── [email protected] .................... └─┬ [email protected] ├── [email protected] └── [email protected] ~$ npm list -g --depth=0 /home/user/.node_modules_global/lib ├── [email protected] └── [email protected]