Running Jest with npx and npm

Get introduced to dev dependencies and running packages using npx and npm.

Adding Jest as a dependency

Use npm to install the jest package:

$ npm install --save-dev jest@23.6.0 # 1
npm created a lockfile as package-lock.json. You should commit this file. # 2

+ jest@23.6.0
added 538 packages from 269 contributors in 24.446s # 3
  1. The --save-dev flag tells npm, “I want to use the jest package for development only. My project doesn’t need it at runtime.” The jest@23.6.0 means “I want version 23.6.0 of the jest package.”

  2. For reasons that are too involved to go into here, the information in package.json isn’t enough for two machines to be guaranteed to get the same node_modules, even if you specify exact versions for all dependencies. That’s why npm creates the lockfile. If you’re curious about the details, check out the npm docs.

  3. The “538 packages” figure is striking, but it’s propper for Node packages: You depend on one package, which depends on several other packages, which each, in turn, depend on several more, and so on. npm recursively installs all of them. If you’re curious to see what all these packages are, take a peek at the freshly created node_modules directory.

Try and install the package in the terminal below. You can also read the package.json file using cat package.json.

Get hands-on with 1200+ tech skills courses.