Introduction and usage of NPM (Node Package Manager)
Introduction
NPM (Node Package Manager, Hoimepage: https://www.npmjs.com/) is a tool like Maven or Gradle for managing dependencies for a Javascript project. Maven and Gradle can download dependencies from a Maven repository. NPM downloads Javascript-dependencies from the NPM repository at https://npmjs.com/ (or other repositories).
Usage
Version
Check that you installed npm correctly by getting its version:
$ npm -v
10.1.0
Initialize Project
To use npm managed packages in your javascript project, you have to initialize it with npm init
in your project’s directory.
$ cd npm-test-project
$ npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.
See `npm help init` for definitive documentation on these fields
and exactly what they do.
Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.
Press ^C at any time to quit.
package name: (npm-test-project)
version: (1.0.0)
description:
entry point: (index.js)
test command:
git repository:
keywords:
author:
license: (ISC)
About to write to D:\projects-labs\npm-test-project\package.json:
{
"name": "npm-test-project",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
Is this OK? (yes)
$
We accepted all defaults.
The following package.json
file has been created:
{
"name": "npm-test-project",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
Add Dependency
Dependencies are added to your project using npm install name-of-package
:
Example: Add jQuery as dependency to your project:
$ npm install jquery
If you get this error:
npm ERR! The operation was rejected by your operating system.
npm ERR! It's possible that the file was already in use (by a text editor or antivirus),
npm ERR! or that you lack permissions to access it.
npm ERR!
npm ERR! If you believe this might be a permissions issue, please double-check the
npm ERR! permissions of the file and its containing directories, or try running
npm ERR! the command again as root/Administrator.
npm ERR! A complete log of this run can be found in: C:\Users\YOUR_USERNAME\AppData\Local\npm-cache\_logs\2023-11-17T11_35_11_348Z-debug-0.log
The logfile states:
0 verbose cli D:\dev\nodejs\node.exe D:\dev\nodejs\node_modules\npm\bin\npm-cli.js
1 info using npm@10.1.0
2 info using node@v20.9.0
3 timing npm:load:whichnode Completed in 3ms
4 timing config:load:defaults Completed in 2ms
5 timing config:load:file:D:\dev\nodejs\node_modules\npm\npmrc Completed in 4ms
...
9 timing config:load:file:D:\projects-labs\npm-test-project\.npmrc Completed in 0ms
...
11 timing config:load:file:C:\Users\YOUR_USERNAME\.npmrc Completed in 1ms
...
13 timing config:load:file:C:\Users\YOUR_USERNAME\AppData\Roaming\npm\etc\npmrc Completed in 0ms
We have a look into these npm config files:
D:\dev\nodejs\node_modules\npm\npmrc
:prefix=${APPDATA}\npm
D:\projects-labs\npm-test-project\.npmrc
: - does not exist -C:\Users\YOUR_USERNAME\.npmrc
: - does not exist -C:\Users\YOUR_USERNAME\AppData\Roaming\npm\etc\npmrc
: - does not exist -
So it looks the configuration from D:\dev\nodejs\node_modules\npm\npmrc
has been loaded, configuring prefix
to ${APPDATA}\npm
.
In the end the problem was: company’s proxy was not configured…
You can configure a proxy in C:\Users\YOUR_USERNAME\.npmrc
according to this documentation https://docs.npmjs.com/cli/v10/using-npm/config (using “proxy”, “http_proxy”, “https_proxy”, “no_proxy”).
After this the install was successful:
$ npm install jquery
added 1 package, and audited 2 packages in 4s
found 0 vulnerabilities
Package jquery
has been downloaded and been added to new node_modules
folder.
The node_modules
folder is not subject to be checked in into git (or other VCS/versioning system)! (DO NOT CHECK IN!)
If you check out from VCS, it is recreated when you execute npm install
again.
In package.json
jQuery has been added as dependency ("jquery": "^3.7.1"
):
{
"name": "npm-test-project",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"jquery": "^3.7.1"
}
}
It downloaded the latest version “3.7.1”. The caret “^” states that our project depends on version “3.7.1” OR HIGHER VERRSION 3.x.x.
In the newly created package-lock.json
it has been added as dependency:
{
"name": "npm-test-project",
"version": "1.0.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "npm-test-project",
"version": "1.0.0",
"license": "ISC",
"dependencies": {
"jquery": "^3.7.1"
}
},
"node_modules/jquery": {
"version": "3.7.1",
"resolved": "https://registry.npmjs.org/jquery/-/jquery-3.7.1.tgz",
"integrity": "sha512-m4avr8yL8kmFN8psrbFFFmB/If14iN5o9nw/NgnnM+kybDJpRsAynV2BsfpTYrTRysYUdADVD7CkUUizgkpLfg=="
}
}
}
The package-lock.json
is subject to be checked in! It pins the versions to exactly one version (“3.7.1”). Every time you update dependencies the lock-file changes, too, and you check it in again.
Happy NPMing!