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 :
To install the latest version do :
| latest | |
|---|---|
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 :
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.
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 :
You can alter it like this, but it does means a number of changes.
| permanent | |
|---|---|
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 | |
|---|---|
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 | |
|---|---|
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 | |
|---|---|
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)