DevOps Tools/Repository Management/npm

From r00tedvw.com wiki
Jump to: navigation, search

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]

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
Personal tools
Namespaces

Variants
Actions
Navigation
Mediawiki
Confluence
DevOps Tools
Ubuntu
Ubuntu 22
Mac OSX
Oracle Linux
AWS
Windows
OpenVPN
Grafana
InfluxDB2
TrueNas
OwnCloud
Pivotal
osTicket
OTRS
phpBB
WordPress
VmWare ESXI 5.1
Crypto currencies
HTML
CSS
Python
Java Script
PHP
Raspberry Pi
Canvas LMS
Kaltura Media Server
Plex Media Server
MetaSploit
Zoneminder
ShinobiCE
Photoshop CS2
Fortinet
Uploaded
Certifications
General Info
Games
Meal Plans
NC Statutes
2020 Election
Volkswagen
Covid
NCDMV
Toolbox