Getting Started with NPM
By: Harsh Kumar Khatri
Tags: NPM, Npm Packages, Node Js Npm, Getting Started With Npm, Node

Photo by Paul Esch-Laurent on Unsplash

Hey everyone in this article we will be seeing about npm and how a beginner could get started with npm.

This article will be covering the following given things:-

Initializing the package.json file.

Adding node packages

Managing global directories.

Updating packages

Removing a package

Semantic versioning

Package-lock.json

Working with NPM Cache

NPM audit

NPM alternatives

Initializing the package.json file

For initializing the package .json file you need to have a ‘.js’ file in your project folder which will be acting as your main run/resource file. I have linked the one I used in my learnings. You can extract them out and then use the extracted folder as your main project directory.

Project files

Once you have set up the things, open up a terminal in the editor which you are using, I prefer VS Code, and in the terminal type, the command is given below.

npm init

Once the command will be executed you will be asked for package name, description, main file, etc. For most of the things, the default will be there you just need to press on entering the key and the input will be taken automatically. This will successfully create a package.json file in your project directory which will be having the details which were filled by you after running the above command.

This is are the things that will be there inside the JSON file.

Adding Node Packages

Once you have successfully got a package.json file in your project, you can now head to install the packages or dependencies in your project.

For installing a package open up a terminal and run the command given below.

npm install <package-name>

Make sure to replace the <package-name> with your package name.

Install express

Once the package is installed in your project you can then see an extra folder which is there in your main project directory with the name of node_modules. It will be having all the files related to the packages which you will be installed in your project. Another file package-lock.json will also be formed after you have installed the package successfully. This file is generated to avoid differences in installed dependencies on different environments and to generate the same results on every environment we should use the package-lock.json file to install dependencies. (More info about this further)You will also see a change in your main package.json file which will have a new “dependencies key and like the value, it has another key pair having the package name as the key and the version as the value.

For installing a package with a specific version you can use the command given below.

npm install <package-name>@<version>

There is another way of installing the dependencies/package which will install the packages in the development environment and those installed dependencies will not be used in your production environment. For installing the dependencies/packages in this mode run the command given below.

npm install babel --save-dev

This will save the installed babel package only for the development environment. You will be seeing a change in the way these are represented in the package.json file.

Managing global directories

Sometimes it needs to install the packages/dependencies globally and we can do that as well.

You might be thinking about where the packages will be stored when stored globally. The below image shows the path/directories to which the packages will be stored.

For installing a package/dependency globally you can run either of the command given below.

npm install -g express

or

npm install -global express

In Linux and MAC, as you do not have direct access to the files in the root directory, you have to add sudo before the command. Once the package is installed it will be visible like this in your directory.

If you still face some errors with permission you can visit the link given below for seeing its solution.

https://docs.npmjs.com/resolving-eacces-permissions-errors-when-installing-packages-globally

Updating packages

There are times when you see warning about the packages which are installed inside your system/project. These warnings are basically due to the version difference occurring in the package. To check if your package version is outdated run the command below for the project packages.

npm outdated

If you want to check if the package you have installed globally has an outdated version you can run the command given below.

npm outdated -g

This will show you a list of outdated packages with their current installed version, latest version, and the required version.

Now if we want to update any of the packages we can simply run the command given below.

npm install <package-name>

This will update the package to the latest version which is available. You can also run the command given below but there are very few chances for that command to succeed.

npm update <package-name>

And if the package is installed globally on your system then you can use the command given below.

npm update <package-name> -g

Removing a package

Till now you have learned about installing and updating a package but what if a package is installed by mistake on your system/directory? You need to remove it, right. For doing this you can run the command given below which will remove the installed package.

npm uninstall <package-name>

And in case the package is installed globally we can run the command given below.

npm uninstall -g <package-name>

Semantic Versioning

This is something related to versioning and helps you understand how the versions are installed for a particular package.

You might be thinking what does the version separated by dots represent. The first character/number generally represents the major release, the second character/number represents the minor release for that particular package and the last character/number represents the patch release which is there to fix the bugs or issues.

There is another thing called caret which is included before the version of the package. If a caret is applied then it works such that it will be installing a package with a particular major release version and will not be depending on the minor release versions.

We can replace caret with a tilde symbol which gives us much more freedom in case of installing a minor release of a package.

If you want to install a particular package having a specific patch release then you can install that with the command given below or could remove the caret from the version in package.json and specify the version which you want.

Semantic versioning is something that most developers miss out on while they are publishing on the node.

Package-lock.json

The package-lock.json file is necessary when the project we are building is to be used/developed by someone else and stops the packaging chain from breaking.

Let’s understand this with an example. Let us assume there is a project with node version 4.15.4 and does not have a package-lock.json file. Now when I clone the project from the repo and installed the node in the root directory, the current version of the node will be installed in this is 4.25.4(assuming). Now are packages are dependent on one another a change in the version of one package will result in the chain of packages breaking and will give us unwanted errors.

Let us now assume that the same package which I cloned has a package-lock.json and the node version required is 4.15.4. Now when I install the node in the root directory, the version which will be installed in this will be 4.15.4 which will not break the package chain and we can work on our project without any errors.

Working with NPM cache

There are certain things that a developer needs to keep in mind when he is working with debugging in npm and dealing with the cache is one of them. Errors pop up when there are cache issues occurring and devs waste tons of hours finding out the errors. To get a report about the cache status you can run the command given below.

npm cache report

This will provide us with the report for the cache covering the package updates available and the cache issue.

If you want to clear the cache out you can just run the command given below which will force clear the cache.

npm cache clean --force

NPM Audit

The npm audit is a command which runs automatically when we install any of the packages and this checks if the package which we are installing is working fine and let us know if we are facing any issues while the installation.

For having the audit work correctly we need to have npm version 6 or above.

You can check your npm version by the command given below.

npm -v

If the version is less than 6 you can run the command given below to update the npm package and install it globally.

sudo npm install npm -g

Once you have the correct version of npm you can run the command given below to see if you have any issues with the packages.

npm audit

The issues which will be visible to you will be in different categories like high and low. If the vulnerability is high then you need to fix that issue ASAP.

For fixing the issue you can simply run the command given below or can install the particular package again to avoid errors/issues.

npm adit fix

NPM Alternatives

Though npm bundles with it a lot of packages and various features but you can always explore the alternatives npm has to see what fits you the best. Two of the most popular alternative for npm are:-

If you have got to learn something from this article then do share it with others so that everyone can learn and we can grow together.

If you have any doubts then you can comment them down below. I would be happy to help. Till then you can connect with me on Linkedin:- Harsh Kumar Khatri | LinkedIn