DevOps Tools/Repository Management/npm

From r00tedvw.com wiki
Jump to: navigation, search

npm

Contents

 [hide

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
├─┬ npm@6.9.0
│ ├── abbrev@1.1.1
│ ├── ansicolors@0.3.2
│ ├── ansistyles@0.1.3
│ ├── aproba@2.0.0
│ ├── archy@1.0.0
....................
└─┬ uglify-js@3.5.3
  ├── commander@2.19.0
  └── source-map@0.6.1

~$ npm list -g --depth=0
/home/user/.node_modules_global/lib
├── npm@6.9.0
└── uglify-js@3.5.3

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 nodedemo@1.0.0 No description
npm WARN nodedemo@1.0.0 No repository field.

+ underscore@1.9.1
added 1 package from 1 contributor and audited 1 package in 7.264s
found 0 vulnerabilities
Personal tools
Namespaces

Variants
Actions
Navigation
Mediawiki