A Guide to NPM modules and there significance in backend development

·

6 min read

NPM Modules

NPM stands for node package manager. It provides 1000s of packages to use in our application.

The difference between a Node common core module and npm package is that npm packages are Node modules that are created by 3rd parties or in other words other developers.

To find the documentation of npm visit here After visiting the site go to CLI documentation.

Install a npm package globally

The syntax for installing a node package gloabally is :

npm install node_package_name

Now install word can be replaced by add or i.

npm i package_name
npm add package_name

The above two will also work.

We have been writing node filename again and again to run our node server. We can install a node package much like Live Server in React. We will install it globally so whenever we save our files our server can run again.

The name of npm package is nodemon and as we mentioned the syntax above, you have to write the following in the terminal and press Enter.

npm i nodemon -g

nodemon by default will look for index.js but if you want it to search for another file while running you need to write the following in the terminal after it is installed.

nodemon file_name

If you get this error

"running scripts is disabled on this system"

Go to the npm folder in C:\Users\HP\AppData\Roaming\npm and delete the nodemon powershell file and re-run the command in the VS code terminal. It will work.

Initialize the npm

You might have seen or heard of package.json file in your folder structure when working on any web projects.

You can create a one for your project from scractch using the command

npm init

It will ask a bunch of questions that you can answer or choose yes everytime by hitting Enter.

To accept all default answers to questions change command to

 npm -y init

Once paackage.json is created you can now add/install more npm packages and they will be added as dependencies.

Consider we are installing a data package

npm -i data-fns

This will add a dependency in your package.json like this

 "dependencies": {
    "date-fns": "^2.29.3"
  }

After this install you will see a package-lock.json and node_modules folder in your directory.

What are these files and why we should know about them?

Well don't change anything in package-lock.json as it is maintained by npm.

In case of node_modules, it contains the information about the npm package that you have installed and this file becomes very large as we keep installing new modules as per our requirement.

These packages can be downloaded on the local system easily using the information from package.json so we create a .gitignore file and first thing that we write there is node_modules

To read the packages from package.json we use

npm install

This command is useful when we clone a remote repository.

Save a dev dependency using terminal

The following command will be used to save nodemon as a dev dependency:

npm i nodemon --save-dev

Now this can be written in shorthand format as well:

npm i nodemon -D

You can notice that replacing nodemon with any other package name will save the dev dependency as well so you can use this command anytime you want to save a npm package as dev dependency.

Working with scripts

In package.json file you will see scripts portion. If you are familiar with React you would have seen many scripts like start, build, test etc.

These words run commands that we specify beside them. For example if you see

"scripts": {
    "start" : "node index",
    "dev": "nodemon index"
  },

Now if you will hit Enter after writing ''' npm run start ''' the terminal will read it as ''' npm node index ''' (start are among few words that don't need 'run').

Similarly if you will run npm run dev , this will mean npm nodemon index .

This comes handy when you to write large number of commands. You can simply write a custom command that will do the same task a vrey long set of commands. Hope that makes sense.

The above dev and start command was added manually just to tell you that these scripts can be written according to us.

Now we installed an npm package name uuid which generates a new id for every entry.

We can import it in many different ways. Like these two where we are working with version v4

const uuild = require('uuid');

console.log(uuild.v4())

or

const {v4:uuid} = require('uuid');

In the second example v4 has been imported as uuid!

After importing use the following code in index.js and run the dev script. You will see a different string being logged every time whenever you make some changes in the file and run again.

console.log(uuid());

This different ids every time is useful for times when we want to log some actions everytime there is any change like error, request etc.

Checking package.json and understanding the semantic numbers

When looking in package.json under dependencies you will see something like this :

"uuid": "^9.0.2"

Now what does the numbering mean?

9 means a major version release 0 means a minor version release 2 means a patch

The ^ before 9 means go ahead and do an update to the minor version or the path if needed but do not update the major version . Making changes to major version can lead your app to break.

If you are not specifying ^ before 9.0.2 it means only version 9.0.2 should be used for the particular project and "no other version will work".

Now instead of "^9.0.2" you see something like this

"uuid": "*"

This will mean that update every version, every path, everytime the project is being used and use the latest version of the dependency installed. This is not always safe.

If you want to install a specific version of an npm module write in this format :

npm i npm_name@version_number

Check for updates of npm

npm update

Uninstall a dependency

You can uninstall any dependency unsing the following commands. Hit enter after writing them in the terminal.

1.

npm uninstall dependency_name

2.

npm un dependency_name

3.

npm rm dependency_name

Now in this blog we mentioned about nodemon which is a dev dependency. Whenever we have to remove a dev dependency we need to specify that to the terminal in the following manner

npm rm nodemon -D

This command will change the dependencies and devDependencies section of the package.json but if you have written a custom script it will still remain the same so it is very important to go and update the scripts after removing or uninstalling a dependency.

Thanks to free lecture of Dave Gray. You can check his video here