Components: website, registry, command line (CLI)
Website: discover package, setup profile
Registry: package database
CLI:
get packages
get standalone tools
run without download with npx
...
Install node.js
npm install npm@latest -g.
nvm - version manager
EACCES - permission errors
Install
locally
when to depend from own module for 'require',
npm install
npm install --save //- add to package.json as dependency
npm install --save-dev // add as devDependency
will install into node_modules dir
version
if package.json exists, the latest version that satisfies the semver rule
product.feature_not_breaking_existing_features.bugfix
or latest
npm uninstall //- remove
npm uninstall --save xxx //- remove from the dependencies in package.json
globally
command line tool, install globally "npm install -g xxx"
NEVER "sudo", see https://medium.com/@ExplosionPills/dont-use-sudo-with-npm-still-66e609f5f92
For tools, project level installation preferred over global installation, use npx (or yarn) to run locally installed packages
For global installation, use npm's 'prefix' configuration, (see npm help 7 config)
$ npm config set prefix ~/.npm
Or just edit ~/.npmrc and include 'prefix=~/.npm'
Also add the bin path
"npm init" creates the file
name: all lower case, no space, - & _ allowed
scoped: "@scope/name"
only scope owner can publish something under scope
install under a sub folder in scope folder
use require("@scope/name")
version: x.x.x
description
main: always index.js
scripts : by default an empty test script
keywords :
author
license
bugs
homepage
dependencies - required in production
devDependencies - needed for development and testing
npm update
npm outdated - check
npm update -g xxx
npm outdated -g --depth=0
npm install npm@latest -g
npm uninstall -g xxx
Package & Module
package
file or dir or zip or git...
contains package.json
most packages are also modules
module
can be loaded by Node.js require(...)
can be
a folder with package.json that contains a main field
folder with an index.js inside
a javascript file
the node_modules folder is the place node.js looks for a module
To create module
npm init //- create package.json
create index.js
write export.someThing = something
create a test.js which requires the package and test
Policy - be respectful
Create user account : npm adduser
or login : npm login
(npm whoami)
Review everything in the package directory. All will be included unless in .gitignore or .npmignore
Choose a name: unique, not already owned, not spelled like another, not misleading, not offensive
Documentation: readme.md
npm publish
Workspace: https://docs.npmjs.com/cli/v7/using-npm/workspaces
Pack: npm pack
Link: npm link
Comparator = operator + version:
< <= > >= =
Can be joined:
by whitespace (intersection)
by || union
Advanced syntax:
huphen range X.Y.Z - A.B.C : >=X.Y.Z <=A.B.C
x-range (x, X, *): match any
tilde range ~ (match and greater)
~1.2.3 := >=1.2.3 <1.3.0
~1.2 := >=1.2.0 <1.(2+1).0 := >=1.2.0 <1.3.0
~1 := >=1.0.0 <(1+1).0.0 := >=1.0.0 <2.0.0
caret ranges ^:
allows patch and minor updates for versions 1.0.0 and above, patch updates for versions 0.X >=0.1.0, and no updates for versions 0.0.X.
reason: 0.x version treated x as "breaking change"
Pre-release tags
Only allowed if comparator has the same tag