Node.js

Node.js is a server-side JavaScript environment that also lets you run JavaScript in the terminal, which is pretty exciting! So far in your projects you have mainly been using JavaScript on the client-side (in the browser).

npm

npm is a package manager that is installed with Node.js. It's used to download, build, and install packages for use in our programs.

A package manager lets you use other people's JavaScript modules. We don't want to write everything from scratch or we'll be here forever! One of the wonderful things with open source programming is the ability to use and publish code to help achieve certain things. By using modules and libraries we can get to doing the fun stuff faster.

npm reads the package.json file in the root directory of your project. It contains one JSON object which is used to provide configuration. Of particular interest are the scripts and dependencies properties.

Useful npm commands to understand:

  • npm install - installs all the dependencies listed in your package.json file into a node_modules folder
  • npm start - used to launch the application
  • npm test - used to launch your test suite
  • npm run dev - looks for a script in your package.json called 'dev' (in this example) and runs that script

Notes:

  • You can learn more about npm scripts in the npm docs
  • You will see frequent references to Yarn, another package manager for Node. We use npm at Dev Academy, but the two share many similarities and you will likely encounter both in the workplace and open source projects.
  • If you want to reinstall your node_modules, you can first delete the folder with rm -rf node_modules and then npm install.
  • Prevent your node_modules folder from being committed by adding it to your .gitignore file.

Isaac Newton Quote

Make sure you're using an up to date version of Node and npm:

which node
node -v
npm -v

Use NVM to keep your versions of Node and npm up to date.

Resources