All notes
Npm

Walkthrough

Good References

Commands

bin


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

`npm bin`/webpack

install

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.

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.


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

# Install all modules listed as dependencies in package.json.
npm install

# -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

--save-dev

segmentfault.com.

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

通过这些命令,我们会得到一个新的package.json。然后再做一个试验就懂得了区别:删除node_modules目录,然后执行 npm install --production,可以看到,npm只帮我们自动安装package.json中dependencies部分的模块;如果执行npm install ,则package.json中指定的dependencies和devDependencies都会被自动安装进来。

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

Use mirrors

ustc.edu.cn: npm help.

Add in ~/.npmrc


registry=https://registry.npm.taobao.org

# or

registry=http://npmreg.mirrors.ustc.edu.cn/

若不想将本源设置为默认源,只想使用本源安装某个软件包,可在安装包时采用以下用法


npm --registry http://npmreg.mirrors.ustc.edu.cn install packageName

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

Use china mirror:


npm install -g npmrc
npmrc -r cn
# Using http://r.cnpmjs.org/ registry.
# Or
npm set registry http://r.cnpmjs.org/

# https://cnpmjs.org/ cnpmjs.org: 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,[email protected]}, C{[email protected]}, this algorithm produces:

# Dependencies will be added as close to the top as is possible without breaking any other modules
A
+-- B
+-- C
   `-- [email protected]
+-- [email protected]

# Because B's [email protected] will be installed in the top level, C now has to install [email protected] 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 [email protected] that is already present anywhere in the tree of package folder ancestors, so it will become:
A -> B -> A' -> B' -> (A, stopped here)

ls


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

# Remove the unmatched version and install the matched version.
npm rm [email protected]
npm i -D [email protected]

Publish



##########
# https://docs.npmjs.com/getting-started/publishing-npm-packages

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

Restart

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

prerestart

prestop
stop
poststop

restart

prestart
start
poststart

postrestart

Start

npmjs.com.

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.

update

As of [email protected], 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

docs.npmjs.com/cli/update.



# Caret Dependencies
"dep1": "^1.1.1"
# npm update will install [email protected], 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 [email protected]

FAQ

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


//package.json

"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