NPM basics in less than 10 minutes

Rajat Sharma
The Startup
Published in
5 min readOct 31, 2019

NPM stands for Node Package Manager. As the name suggests, it is a package manager for Javascript. It comes pre-installed with NodeJS and we don’t need to configure it explicitly. Also, we don’t need any NodeJS experience to start working with NPM.

NPM has a registry where it contains all the different JS libraries published from all around the globe. We then just ask NPM to fetch us our required JS library from its registry and save it on our system, locally or globally(more on this later).

Also, you must have heard words like packages or modules, but they are kind of a same thing referring to the JS libraries like JQuery, Bootstrap etc.

Moreover, we are provided with a CLI(command line interface) which helps in interacting with the NPM for installing, updating and uninstalling packages, and managing dependencies.

Here in this article we will discuss about:

  • Installing NPM
  • package.json file
  • Different environment dependencies
  • Global and Local packages
  • Versioning of modules
  • NPM scripts

If you are here for a specific topic from above, feel free to skip to that particular concept.

Installing NPM:

As said earlier, NPM comes pre-installed with NodeJS. So just head on to https://nodejs.org/en/download/ and install node.

Once installed, run command:

npm -v

If everything is fine, you will see an output showing the NPM version, else you will get an error stating “npm command not found”.

Package.json file:

This file is like the metadata for your application. It will hold all the dependencies(packages or modules) that your application requires for successful run.

Also, it is home to all the NPM scripts(discussed later) that you may build to run or configure your application.

So how do you create it, just head into the folder you want your application, and run command:

npm init

This will ask you bunch of questions like name of the author, description (bla bla bla), keep on pressing enter for defaults, or just use command

npm init -y 

to setup all the default options.

After running this command, you will have a package.json file with all the default properties set.

To add a package or module that you require in your application, type:

npm install <packageName> --save.

This will install the specified package and add the entry to package.json under dependencies

You will see a “node_modules” folder after running npm install. This folder contains all the packages or modules you have mentioned that will be required by your application. There will be bunch of other stuff in it as well and that’s because it includes all the transitive dependencies, which means if your app requires Module A which further requires Module B, npm will install both of these modules in “node_modules” folder.

Different environment dependencies:

There will be multiple scenarios where you want a particular library for your local purpose( such as testing) but don’t need these libraries in final release. For this purpose we have Dev-Dependencies. To add a dependency as a dev-dependency, run:

npm install <packageName> --save-dev

You will see another field named “devDependencies” inside your package.json which will hold your specified package.

To install the dependencies present in package.json, just run command

npm install

This command will install both the normal as well as dev dependencies.

To just install the dependencies required for production, run it with production flag:

npm install --production

So only include package as your dev dependencies that are required for local environment purposes.

Global and Local packages:

There will be some modules that you will require for your every application. So rather than installing them again and again inside each application, we install such modules globally.

npm install -g <packageName>

This will install this module in our global “node_modules” folder(use command “npm root -g” to know your global “node_modules” folder) and we don’t need to include these modules anymore in our applications.

By default all our dependencies are installed locally, inside the local “node_modules” folder where we run the command and these modules cannot be accessed anywhere else.

Package Versioning:

Whenever you install a package, you will see the dependency in package.json as “~4.17.1”.

Each of these character have a meaning.

Package Versioning(online source)

As the image shows, the last digit is for a bug fix, so whenever module owners fix a bug they increase this version and release it.

The middle one is the minor version, which is like some enhancements over current release with some added features.

The major version, is a complete new(almost) version of the module which might break your application if you switch to the latest one.

Also the flags in front such as

^ : this will install the latest minor version when you run npm install.

~ : this will install the latest patch(bug fix) version when you run npm install while keeping minor version same.

* : this will install the absolute latest version of the module.

To install an exact module version:

npm install <packageName>@x.y.z

NPM Scripts:

If you look into your package.json file, you will see a script object. Anything you add into this json, will be a npm script.

An NPM script is a script to automate repetitive task. Suppose you want to start your application with a file with some additional arguments,

just make a start script inside the “scripts” field like

“scripts”:{    “start”: “node index.js <additional_args>”}

and run “npm start” to start your application.

Mostly the scripts come handy for other developer who doesn’t know much about the application specific details and can just look into the script and run the application.

Conclusion :

So we learned some basics of NPM and various ways in which we can play with it. If you want to look at the different packages NPM offers that might suit your requirement, you can go to https://www.npmjs.com/ and search for the module.

Best of luck for all your endeavours.

--

--