Free To Feel

Heading to entrepreneur.

Joshua Chi

NPM NodeJS Environment Setup Best Practice with MACOS

NPM Installation

Do not install NPM with homebrew

Install it from official site

    npm ls -g --depth=0
    sudo rm -rf /usr/local/lib/node_modules
brew uninstall node
sudo rm /usr/local/bin/node
    cd  /usr/local/bin && ls -l | grep "../lib/node_modules/" | awk '{print $9}'| xargs rm
curl -L | sh

Node Installation

Install nvm

curl -o- | bash

Switch version

nvm install stable #Install latest stable node
nvm install 4.2.2 #Install 4.2.2 version
nvm install 0.12.7 #Install 0.12.7 version
nvm use 0 #Will switch to use v0.12.7

Use a smart .npmrc

By default, npm doesn't save installed dependencies to package.json (and you should always track your dependencies!).

If you use the --save flag to auto-update package.json, npm installs the packages with a leading carat (^), putting your modules at risk of drifting to different versions.

One solution is installing packages like this:

$ npm install foobar --save --save-exact

Even better, you can set these options in ~/.npmrc to update your defaults:

$ npm config set save=true
$ npm config set save-exact=true
$ cat ~/.npmrc

Now, npm install foobar will automatically add foobar to package.json and your dependencies won't drift between installs!

If you prefer to keep flexible dependencies in package.json, but still need to lock down dependencies for production, you can alternatively build npm's shrinkwrap into your workflow. This takes a little more effort, but has the added benefit of preserving exact versions of nested dependencies.

Dependencies version management

*: Match any verion
1.1.0: Exactly match the version
~1.1.0: >=1.1.0 && < 1.2.0
^1.1.0: >=1.1.0 && < 2.0.0

nrm switch NPM soruce

$ npm install -g nrm

List all available sources

nrm ls                                                                                                                                    

* npm ----
cnpm ---
taobao -
eu -----
au -----
sl -----
nj -----

* means source you are currently using


Switch to use the one from taobao

nrm use taobao                                                                                                 

Registry has been set to:


comments powered by Disqus