All notes


Good References


About verbose, loglevel

-v: --version

-s, --silent: --loglevel silent
-q, --quiet: --loglevel warn
-d: --loglevel info
-dd, --verbose: --loglevel verbose
-ddd: --loglevel silly


npm bin
# C:\Users\me\projectA\node_modules\.bin

`npm bin`/webpack
$(npm bin)/cypress

config config.

The four relevant files are:
per-project configuration file (/path/to/my/project/.npmrc)
per-user configuration file (defaults to $HOME/.npmrc; configurable via CLI option --userconfig or environment variable $NPM_CONFIG_USERCONFIG)
global configuration file (defaults to $PREFIX/etc/npmrc; configurable via CLI option --globalconfig or environment variable $NPM_CONFIG_GLOBALCONFIG)
npm's built-in configuration file (/path/to/npm/npmrc)

# See a set of configuration parameters that are internal to npm
npm config ls -l

npm config set registry


# Install all modules listed as dependencies in package.json.
# With the --production flag (or when the NODE_ENV environment variable is set to production), npm will not install modules listed in devDependencies.
npm i

npm help install

# -S, --save
# -D, --save-dev
npm i -S/D ...
npm uninstall -S/D ...

# -g or --global: cause npm to install the package globally rather than locally.
npm install -g bower

npm install mygithubuser/myproject
npm install github:mygithubuser/myproject

# Install browserify
npm install -g github:substack/node-browserify

npm list
npm prune # removes packages not depended on by your project according to your package.json
npm outdated

Where is the modules installed

Run npm list -g to see where global libraries are installed. On Unix systems they are normally placed in /usr/local/lib/node or /usr/local/lib/node_modules when installed globally.

Install npm packages globally without sudo on macOS and Linux

Methods from npm:

# Make a directory for global installations:
mkdir ~/.npm-global
# Configure npm to use the new directory path:
npm config set prefix '~/.npm-global'
# Open or create a ~/.profile file and add this line:
export PATH=~/.npm-global/bin:$PATH
# Back on the command line, update your system variables:
source ~/.profile
# Test: Download a package globally without using sudo.
npm install -g jshint

# Instead of steps 2-4, you can use the corresponding ENV variable (e.g. if you don't want to modify ~/.profile):

# Tip: Consider npx
# If you are using npm version 5.2 or greater, explore npx as an alternative way to run global commands, especially if you just need a command occassionally. Click here to read an excellent article about npx.

mkdir "${HOME}/.npm-packages"

# Indicate to npm where to store globally installed packages. In your ~/.npmrc file add:

# Ensure npm will find installed binaries and man pages. Add the following to your .bashrc/.zshrc:
# Unset manpath so we can inherit from /etc/manpath via the `manpath` command
unset MANPATH # delete if you already modified MANPATH elsewhere in your config
export MANPATH="$NPM_PACKAGES/share/man:$(manpath)"

Difference between NPM and Bower

SO: what is the difference between bower and npm.

NPM is used mainly for server package management, while Bower is used mainly for front end web package management. NPM is the package management tool for node.js.

NPM adopts the tree structure to handle with the dependencies, therefore there will exists several versions of a package (e.g. jQuery). However, Bower uses a flat structure to handle with the dependencies, and there will only be one version of package in the dependent packages managed by Bower.

Bower requires node, npm and git.

-D, --save-dev

Development dependencies are intended as development-only packages, that are unneeded in production. For example: testing packages, webpack or Babel.

When you go in production, if you type npm install and the folder contains a package.json file, they are installed, as npm assumes this is a development deploy. You need to set the --production flag (npm install --production) to avoid installing those development dependencies.

-save 自动把模块和版本号添加到dependencies部分, -save-dev 自动把模块和版本号添加到devdependencies部分。

-S, --save: Package will appear in your dependencies.
-D, --save-dev: Package will appear in your devDependencies.

Use mirrors npm help.

Add in ~/.npmrc


# or



npm --registry install packageName

可以使用 https 替换上述 URL 中的 http。若出现错误,请将 ~/.npmrc 中的用户名密码部分注释掉,并删除缓存 (rm -rf ~/.npm) 重试。

Use china mirror:

npm install -g npmrc
npmrc -r cn
# Using registry.
# Or
npm set registry

# Private npm registry and web for Company.

# If it reports error: no such file as c:\Users\you\AppData\roaming\.npmrc, create one first
touch c:\Users\you\AppData\roaming\.npmrc
npmrc -r cn

wcfNote: you can visit browserify package on the CN mirror like this.

Install algorithm

# For A{B,C}, B{C,D@1}, C{D@2}, this algorithm produces:

# Dependencies will be added as close to the top as is possible without breaking any other modules
+-- B
+-- C
   `-- D@2
+-- D@1

# Because B's D@1 will be installed in the top level, C now has to install D@2 privately for itself.

# To break infinite dependency
# A, A' are two versions of A; B, B' are two versions of B.
A -> B -> A' -> B' -> A -> B -> A' -> B' -> A -> ...
# npm flat-out refuses to install any name@version that is already present anywhere in the tree of package folder ancestors, so it will become:
A -> B -> A' -> B' -> (A, stopped here)


# List all the version and dependencies:
npm ls inferno inferno-devtools

# Remove the unmatched version and install the matched version.
npm rm inferno-devtools@3.10.1
npm i -D inferno-devtools@1.4.0



npm adduser
npm login
npm config ls

# Everything in the directory will be included unless it is ignored by a local .gitignore or .npmignore:
npm publish

########## To Update:
# Will change the version number in package.json:
npm version patch/minor/major
npm publish


Run these commands in "scripts" in the order given below:







npm search chai


Runs an arbitrary command specified in the package's "start" property of its "scripts" object. If no "start" property is specified on the "scripts" object, it will run node server.js.


As of npm@2.6.1, the npm update will only inspect top-level packages. Prior versions of npm would also recursively inspect all dependencies. To get the old behavior, use npm --depth 9999 update.

Dependency syntax: Caret, Tilde

# Caret Dependencies
"dep1": "^1.1.1"
# npm update will install dep1@1.2.2, because 1.2.2 is latest and 1.2.2 satisfies ^1.1.1.

# Tilde Dependencies
"dep1": "~1.1.1"
# Equivalent to >=1.1.1 <1.2.0. npm update will install dep1@1.1.2


npm start NODE_ENV on windows

SO: how can I set NODE_ENV on windows.

Solution 1

REM wcfNote: there should be no space around "&&"!
set NODE_ENV=production&&npm start
REM or
set NODE_ENV=production&&node index.js

Solution 2

Run "npm run start_windows" at command line with your package.json file configured as below


"scripts": {
  "start": "node index.js"
  "start_windows": "set NODE_ENV=production&&node index.js"

Error: Cannot find module 'semver'

SO: npm install cannot find module semver.

sudo pacman -Rs npm
sudo pacman -S npm

Error: Node Sass could not find a binding for your current environment


npm rebuild node-sass