When installing Node, there are two commands that come with it:
npm install random-item in your project. This article is mostly about NPM so I’ll expand on that later. For now, let’s start with the beginning.
The package.json is the file that describes the project, lists its dependencies (i.e random-item in our previous example) and commands. It can contain much more than that though, feel free to have a look at the full list of properties if you are curious. To create this file, you can run the command
npm init at the root of your project. It will guide you through a few questions and create the package.json file at the end of the process.
Most of the information contained by the package.json is useful when publishing your code to the “NPM cloud”. For example, the name, description, keywords, and author is what appears on the dedicated NPM page. You can ignore them if you do not plan to publish a package, they won’t have any effect. At this point, there are only three properties that you need to know:
Tracking development and production dependencies
The tooling I mentioned so far is for development. They are tools you need to install on your computer to compile the code. Ultimately, what you will deploy is the compiled code, not the source code so there is no point in installing them on the production server. To take an example, let’s install the package parcel-bundler as a development dependency:
npm install parcel-bundler --save-dev
Doing so will update our previous package.json to the following:
The parcel-bundler package has been installed and added to the
devDependencies, with a specific version range (^1.11.0). What’s great with installing packages this way is that you know precisely what your project depends on and on which version. More importantly, if you were to git clone the project on another computer, you could install all the required dependencies by simply running
npm install. This command would lookup your package.json file and install all the dependencies with the correct version.
That’s for development but some packages are going to be required for your project to run. Let’s say you are building an application that displays a random background color from an Array of possible values. The code could look like this:
import randomItem from 'random-item';
const colors = [
window.document.body.style.backgroundColor = randomItem(colors);
It uses the random-item package mentioned earlier in this article. In production, the random-item package will still be required for the application to work. In this case, random-item is a production dependency and should be installed as follows:
npm install random-item
npm that random-item is a development dependency with the
--save-dev flag suggests that it’s a production dependency. This command would update the package.json like so:
dependencies are installed when running the
npm install command. Note that it’s pretty uncommon to manually edit those properties. Most of the time, they are updated through the
As you probably guessed,
parcel index.html --port 3000 --log-level 0 --openeverytime you want to start the server.
Create shortcut commands with npm scripts
scripts is a package.json’s property that contains shortcut commands. If you wanted to alias the previous command to “start”, you could do the following:
Now, when running
npm run start npm will look into the package.json’s scripts and execute the command matching “start”, which is:
parcel index.html --port 3000 --log-level 0 --open
If you try to run this command instead of the start script (through
./node_modules/.bin/parcel index.html --port 3000 --log-level 0 --open
The “node_modules” folder contains a “.bin” folder with links to the packages’ executable command (if any). That prefix is automatically added by npm when running scripts so that’s one more reason for using them.
The NPM CLI comes in with a few conventions. For example, the command to start a project is usually “start” and the one to test is “test”. For these commands, you can omit the “run” part in
npm run start so
npm start is equivalent. There are other official conventions but they are not as common as these two.
This article is part of a serie: