How to setup node with nvm on mac

nodejs-new-pantone-black

Update May 7th 2016:

I needed to use an older version of node for a project so I installed nvm or in other words node version manager today, like so:



$ node -v
v6.0.0



$ brew uninstall node
Uninstalling /usr/local/Cellar/node/6.0.0... 
(3,655 files, 38.8M) node 0.12.7_1, 4.1.1, 5.0.0, 5.1.0, 5.10.1, 5.2.0, 
5.6.0, 5.7.0, 5.9.0, 5.9.1 are still installed. 
Remove them all with `brew uninstall --force node`.

$ brew uninstall --force node
Uninstalling node... (33,448 files, 316.7M)

Then,


$ brew update && brew upgrade
$ brew install nvm

On install, you’ll get some instructions for additional configuration like this:

“You should create NVM’s working directory if it doesn’t exist: mkdir ~/.nvm
Add the following to ~/.bash_profile or your desired shell configuration file:”


export NVM_DIR="$HOME/.nvm" 
. "$(brew --prefix nvm)/nvm.sh"

Then on a fresh terminal:


$ nvm help

Finally,

$ nvm install stable

I also created a .nvmrc file at the root and then set it node version 5 (Stable is at 6.1.0) which is what I’d be working with lately.

 


 

This is how I configured npm on my development machine (Mac OS).

npm registry & configuration

For when the mood is to publish npm packages, here’s what there is that goes straight into the package.json file:

$ npm set init.author.name "Marvin Danig"
$ npm set init.author.email "marvin@marvindanig.com"
$ npm set init.author.url "http://marvindanig.com"

And then I created/verified a user in the npm registry, and saved the credentials on the ~/.npmrc file. Here’s how you can do i

This command will prompt for an email and password:

$ npm adduser

To ensure that npm saves installed dependencies to the package.json and maintains an exact version of requirements, set:

$ npm config set save=true
$ npm config set save-exact=true

This is effectively like using the following command each time to install an npm package.

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

My ~/.npmrc file looks something like this:

init.author.name=Marvin Danig
init.author.email=marvin@bubbl.in
init.author.url=https://bubbl.in/marvin
//registry.npmjs.org/:_authToken=*******************
save=true¬
save-exact=true¬
~

npm init

Always start with $ npm init, like so:

$ mkdir my-node-project && cd my-node-project
$ npm init --yes

Default yes to all questions that npm will ask upon initialization. Then open package.json to make changes.

The first thing I set is the ‘engine’ key with the current version of node ($ node -v) in use:

"engines": {
"node": "5.1.0"
}

This makes sure that the environment and dependencies are met exactly everywhere.

Check, Update & Shrinkwrap

I use npm-check-updates, a command-line tool that allows you to upgrade your package.json or bower.json dependencies to the latest versions, regardless of version constraints.

$ npm install -g npm-check-updates
$ npm-check-updates
or
$ ncu

If updates are available pass the -u flag to update:

$ npm-check-updates -u
$ rm -rf node_modules
$ npm install

And then shrinkwrap to lockdown dependencies per updated dependencies for the app with:

$ rm npm-shrinkwrap.json
$ npm shrinkwrap

There you go, those are a few that I find very useful and often. Hope it helps!

Will update with more as I discover them on the go.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s