Skip to content

js npm

Node package manager. We mostly use it to install packages from a central registry at : https://npmjs.com/package/* However you could also place packages yourself, but you need an account then. With that you can create organizations or (private) enterprises to share packages.

Installation

First of all: never use sudo with npm because :

  • you do give root access to someone that put scripts on the npm registry.
  • you can get into a mess where files were installed as root that you can' access.
  • it's not necessary.

When you install a package plainly :

devspace
npm install devspace
ls
check
1
2
3
4
npm -g list | head
/home/kees/.npm/lib
├─┬ @typescript-eslint/eslint-plugin@4.15.1
...

To install the latest version do :

latest
npm install npm@latest -g

Local vs global

You can use npm install or npm -g install.

Locally: npm looks for an existing folder called node_modules in the current directory and creates a folder for each package you install in that folder. If it can't find an existing node_modules folder here, it then looks through the current directory's ancestors until it finds one. If it can't find one, it creates one in the current directory.

Globally: if you use the -g (global) option, the package is installed in a global location. This location varies per Linux distribution, but /usr/local/lib/node_modules/packagename is one example. CentOS7 uses /usr/lib/node_modules/packagename.

I tried to install something in a test directory :

install requests
1
2
3
mkdir test
cd test
npm install requests

But this resulted still in an empty test directory, this was because it had already ran npm install in my home directory before so there was a ~/node_modules. So that is the one npm used. To force the install to be in the current directory, either remove ~/node_modules, or create a node_modules dir in test.

local
1
2
3
4
mkdir test
cd test
mkdir node_modules
npm install requests.

Now you will have a package-lock.json and a number of modules in node_modules installed.

Try not to use ~/node_modules since it will stop the local installs. You do have to reinstall a lot of packages the 'right way' after removing it !!

Global installations go to the same directory :

global home directory
npm config get prefix
/home/kees/.npm

You can alter it like this, but it does means a number of changes.

permanent
1
2
3
4
5
mkdir ~/.npm-global
npm config set prefix ~/.npm-global
# in your ~/.profile
export PATH=~/.npm-global/bin:$PATH
source ~/.profile

All packages installed with npm install -g will end up there, and node will be able to find them.

Setting up npm account

I have not pushed packages myself, we merely use npm as for pulling now...

Troubleshooting

There is a page here that might help : https://docs.npmjs.com/common-errors

Packages

A package is a file or directory described by a package.json file. Packages in the public registry are normally retrieved by their direct name, but getting a personal package would mean urls like

packages
1
2
3
4
git://github.com/user/project.git#commit-ish
git+ssh://user@hostname:project.git#commit-ish
git+http://user@hostname/project/blah.git#commit-ish
git+https://user@hostname/project/blah.git#commit-ish

Scopes

These are the @... formatted strings, for instance the @angular/.... You are granted a scope when you signup for an npm account. Mine would be @klopt undoubtedly.

Here you can see 3 scoped packages and 4 unscoped ones :

scoped
1
2
3
4
5
6
7
8
9
npm list --depth 0
/home/kees
├── @types/google-protobuf@3.7.4
├── @typescript-eslint/eslint-plugin@4.15.1
├── @typescript-eslint/parser@4.15.1
├── castnow@0.6.0
├── degiro@3.1.1
├── devspace@5.8.4
└── unirest@0.6.0

Ignore the npm ERR! extraneous: lines, it means you don't use these packages in your package.json.

Modules

A modules is a directory under node_modules that can be loaded by require(). It's almost the same as a package, but it does not need a package.json file.

require
var req = require('request')

Could be either :

  • A folder with a package.json containing a 'main' field (node_modules/request/package.json)
  • A javascript file. (node_modules/request.js)