Skip to content

Instantly share code, notes, and snippets.

@reillysiemens
Last active June 1, 2022 16:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reillysiemens/9362079 to your computer and use it in GitHub Desktop.
Save reillysiemens/9362079 to your computer and use it in GitHub Desktop.
Working with Node.js

Using nvm

The easiest way to get started with Node.js is to install nvm (Node Version Manager). nvm will allow you to install multiple versions of Node.js and switch between them at any time.

Installing nvm

nvm's installation documentation instructs us to install nvm like so

wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.20.0/install.sh | bash

After that we need only exit our shell and fire it up again (or, better, source your ~/.profile) in order to use nvm.

A Note About Loading nvm

Depending on the type of login shell that you have your ~/.profile may or may not have been loaded. It can be useful to include a similar line in your ~/.bashrc, ~/.zshrc or whatever the cool kids are using these days.

source ~/.nvm/nvm.sh
A Note About Security

It's generally pretty bad practice to go piping the contents of something you've just downloaded directly into a shell all willy-nilly. Something about security risks, compromised accounts, machines, etc... Before you decide to trust someone so blindly why don't you read what they've written first?

wget https://raw.githubusercontent.com/creationix/nvm/v0.20.0/install.sh

will drop nvm's install script into a file called install.sh in your working directory. Inspect it and make sure everything makes sense. When you're done then you can safely run

sh install.sh

Finding Node.js Versions

Let's go get ourselves a Node.js install! First, we'll want to know which versions of Node.js are available. To figure this out go ahead and run

nvm ls-remote

You should see a long list of every available version of Node.js going all the way back to v0.1.x. It's probably best to begin with whatever https://nodejs.org/ lists as the current stable version. In this case it's v0.10.33. Now we can

nvm install 0.10.33

Showing Local Versions of Node.js

To see what versions of Node.js you have installed run

nvm ls

Using Node.js

nvm has graciously installed Node.js v0.10.33 for us. Let's use it!

nvm use 0.10.33

When you install a new version of Node.js through nvm it automatically uses that version, but ordinarily you'll have to tell nvm which version you want to use when you're starting up a new shell. For more advanced usage you can refer to the section on aliases in nvm's usage instructions. If you just want to make sure you always open a new shell with v0.10.33 run

nvm alias default 0.10.33

to set v0.10.33 as the default version of Node.js.

More About nvm

Need to know more about how to use nvm?

nvm help

Using npm

Now that we've got ourselves a working version of Node.js we can have some fun. Node comes with an excellent package manager called npm (Node Packaged Modules). Packages you install via npm are referred to as node modules.

Local Installations

To install a new module (for example, bower) just run

npm install bower

This will locate and download the module from npm's repositories and install to your working directory in a subdirectory called node_modules/. All the node modules you install locally will be found in that directory.

Global Installations

Need to install a node module globally?

npm install -g bower

Now you can that module from the command line (assuming it was meant to be used that way) regardless of what directory you're in.

Working with Node.js Dependencies

Maybe you're not starting from scratch and you've got a project with a package.json file. That's great! You can simply run

npm install

in the directory the package.json file is located in and npm will use it to figure out what node modules it needs to download for you. These modules will be installed to a node_modules directory.

Bonus: Running Local Modules From the Command Line Easily

When you install a local module that can be used as a command line too it isn't accessible in the same way that it would be if you installed it globally. You have to dig down into node_modules/.bin/ to find executables and run them.

OR

You can do THIS and add the following to your ~/.bashrc or ~/.zshrc.

alias npm-exec='PATH=$(npm bin):$PATH'

Now you can run modules specific to the project you're working on like so

npm-exec bower

I prefer to name my alias npe 'cuz it's shorter to type, but to each their own.

More About npm

Need to know more about how to use npm?

npm -h

Using Bower

I know what you're thinking, "Another package manager? What is this, a factory factory?" Bower is package manager for the front-end. It'll help you keep your client-side JavaScript separate from your Node.js and generally just make your project cleaner and more compartmentalized. You'll thank me later.

Installing Bower

Bower is built using Node.js. To install bower (as we did above) we'll use npm

npm install -g bower

Generally it'll be handy to have bower installed globally, but you go ahead and do what you want.

Finding and Installing Bower Packages

Don't know what front-end packages you'll need? Search through bower's package repository until you find something shiny.

Once you've found something you like (D3.js, for example, is awesome) we can install it

bower install d3

This will go out and download the package you want to a directory in your working directory called bower_components. Take a peek! I'll wait.

Working with Bower Dependencies

Back? Good. Just like npm, bower can find dependencies of your project for you using a bower.json file. Simply run

bower install

in the directory that your bower.json file is located in and bower will do the work for you. Just like before those dependencies will hang out in a bower_components directory.

More About Bower

Need to know more about how to use bower?

bower help
Alternatives to Bower

Recently, some people seem to be enjoying Duo. Doesn't hurt to check it out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment