Topics, Tools and Terms
Node packages are managed through Node Package Manager, commonly called just 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 Node Version Manager, 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.
The repository for the example applications is available at github.com/vanilla-project/node-command-line.
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.
File and directory names are in lower case and files match the classes or modules they contain.
For example the class
Vanilla should be contained in file
Tests match their production code file names with a
.spec suffix, e.g. tests for code in
src/vanilla.js should be written in
The repository for the example applications is available at https://github.com/vanilla-project/node-command-line.
The main application consists of basically three files:
index.jsis the main executable that instantiates and runs:
src/app.jscontains the main application that uses:
src/example.jswhich contains only one method that returns a string.
Running the Application
To run the application we can execute
This should print the text “Node Example”.
$: npm start Node Example
Running the Tests
To run the tests we execute
npm test which then looks for all files inside directory
test and runs them.
The output should look like the following:
$: npm test > firstname.lastname@example.org test /Users/nickbdyer/projects/beer_idea/node-command-line > mocha test/ --reporter dot ․․ 2 passing (7ms)
The test for class
Example is only verifying the return value of one method.
App on the other hand is tested via a test-double that gets injected.
This allows us to spy on the output of it.
It is not trivial to create a Duplex Stream in Node that behaves identically to
Console takes a stream in it’s constructor we have used
Writable Stream to a file that is then read back in for the test assertion.
In the executable
index.js we then inject
new Console, which is Node’s variable for its standard output.