Free To Feel

Heading to entrepreneur.


Joshua Chi
Github

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
or
sudo rm /usr/local/bin/node
    cd  /usr/local/bin && ls -l | grep "../lib/node_modules/" | awk '{print $9}'| xargs rm
curl -L https://www.npmjs.com/install.sh | sh

Node Installation

Install nvm

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.29.0/install.sh | 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

http://www.tuicool.com/articles/nYjqeu

Install
$ npm install -g nrm
Usage

List all available sources

nrm ls                                                                                                                                    

* npm ---- https://registry.npmjs.org/
cnpm --- http://r.cnpmjs.org/
taobao - http://registry.npm.taobao.org/
eu ----- http://registry.npmjs.eu/
au ----- http://registry.npmjs.org.au/
sl ----- http://npm.strongloop.com/
nj ----- https://registry.nodejitsu.com/

* means source you are currently using

Switch

Switch to use the one from taobao

nrm use taobao                                                                                                 

Registry has been set to: http://registry.npm.taobao.org/

References

comments powered by Disqus