Topics, Tools and Terms
Node packages are managed through a tool called npm. It comes with the Node.js language distribution itself.
npm lets us run install commands to add packages to our system, for example
npm install express will install Express.js on our system.
Having to install dependencies by hand quickly becomes tedious and time consuming.
npm helps us manage our dependencies in a file called a
The content of that file is a list of packages we need for a given project.
package.json looks like this:
We have more than just dependency management captured here.
As you can see there is a section for scripts, scripts can be run from the command line via the
npm command, for example
npm run lint or
Custom commands need the word
npm, but default commands like
start can be run with just
The second section adds project dependencies with versions, similar to other package managers. The third adds dependencies that are only required for the development of the project, not for running it in production.
When faced with a new project and an existing
package.json, have npm install everything needed by running
This will download all dependencies specified in the
Once you start working on more than one Node.js project a version manager will prove very beneficial.
Without a version manager we’d need to always install the currently needed version of Node.js for the project we’re working on. This isn’t so much a problem at the beginning, but once we work on one or two different projects that use different versions of Node it’s helpful to be able to just switch between versions.
We recommend using a version manager in order to be able to use different node versions on your system.
The most popular and well maintained version manager available is: NVM.
The main difference to consider is that Mocha and Karma are test runners, so they need to be combined with seperate assertion and mocking libraries (Chai, Sinon respectively) in order to function. Jasmine comes with everything out of the box so you don’t need to worry about extra libraries.
All frameworks are mostly equally powerful and it boils down to personal preference which one you pick.
In our guides we will be using Mocha and Chai as the default testing framework.
A typical directory structure for a node.js command line project consists of a
src directory that contains all source files and a
test directory that includes all tests.
Since the community of Node.js users is so large, it is difficult to see a standard in the way apps are structured. It is easy to find node command line applications that don’t follow the folder structure below.
We provided a working example of a minimal project on Github.