DevOps Tools/Repository Management/npm
(→Local) |
|||
Line 70: | Line 70: | ||
<nowiki>~$ npm init --y</nowiki> | <nowiki>~$ npm init --y</nowiki> | ||
Whenever you install a new package, it will update the <code>package.json</code> file. | Whenever you install a new package, it will update the <code>package.json</code> file. | ||
− | <nowiki>~ | + | <nowiki>~$ npm install underscore |
npm notice created a lockfile as package-lock.json. You should commit this file. | npm notice created a lockfile as package-lock.json. You should commit this file. | ||
npm WARN [email protected] No description | npm WARN [email protected] No description |
Latest revision as of 16:29, 15 October 2019
npm
Contents |
[edit] 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.
[edit] Installation
https://github.com/nodesource/distributions/blob/master/README.md
[edit] Ubuntu
~$ curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash - ~$ sudo apt-get install -y nodejs
[edit] CentOS
~$ sudo curl -sL https://rpm.nodesource.com/setup_12.x | bash -
[edit] 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
[edit] Update
[edit] CentOS
~$ npm install npm@latest -g
[edit] Node Modules
[edit] 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.
[edit] Package Installation
[edit] 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]
[edit] Local
Most of the time you will install packages locally using a package.json
file.
Create a package.json
file.
~$ npm init --y
Whenever you install a new package, it will update the package.json
file.
~$ npm install underscore npm notice created a lockfile as package-lock.json. You should commit this file. npm WARN [email protected] No description npm WARN [email protected] No repository field. + [email protected] added 1 package from 1 contributor and audited 1 package in 7.264s found 0 vulnerabilities