npm Dependencies and devDependencies

Neelakshi Lahiri
Last Updated: May 13, 2022

Introduction

All of us have gone to the market to shop at some time or the other. There, we have specific stores for certain items. 

For example, we buy vegetables from one shop, fruits from another and rice and lentils from yet another shop. Thus, a particular item is selected from a specific shop only. 

I’m pretty sure you’re wondering why we’re discussing shopping. 

Well, a concept similar to shopping in a market exists in coding. Can you guess what it is?

Source: giphy

 

If you’re thinking about packages, you are correct!

With the example above, I’m pretty sure we’re clear with what packages are, so let’s move on to dependencies. 

In Node.js, there exists a package manager called npm (node package manager). When we install packages from npm, they are installed as dependencies. 

Now, two types of dependencies are npm dependencies and devDependencies. In this article, we’ll learn what they are and the difference between them. 

NPM Dependencies

NPM dependencies are all the packages that are used in a project. We can also say that npm dependencies are the packages needed for an application to work in production. 

Now, you may be wondering how do we install a package as a dependency. Let’s see the syntax for that. 

npm install <package-name--save

 Now, the package.json file contains all the dependencies used in a web application. So, if we checked it after installing a dependency, it would contain the following:

"dependencies": {"moment":"^2.23.0"}

 

devDependencies

devDependencies are the dependencies that are used only in the development stage and not in production. 

Now, you’re probably wondering why we need to do that. To understand the reason, let us consider an example. 

The bootstrap module needs to be installed only in the development phase and not during production. So, it will be installed as a devDependency. 

Now that we know what a devDependency is let’s see the syntax to install one. 

npm install <package-name--save-dev

 

devDependencies are also present in the package.json as shown below:

"devDependencies": {“bootstrap":"^4.3.1"}

 

Difference Between npm Dependencies and devDependencies

After reading the article so far, I’m pretty sure you’ve already figured out the difference between npm dependencies and devDependencies. 

Don’t worry if you haven’t, because we’ll discuss what it is in this section.

The difference between npm dependencies and devDependencies is that npm dependencies are used for a project to work in production, while devDependencies are used only in the development phase. Because of this difference, there is a noticeable difference in the syntax. 

Now that we know all about npm dependencies and devDependencies let’s recap what we learnt by going through some frequently asked questions.

Frequently Asked Questions

  1. What are dependencies?
    When we install packages from npm, they are installed as dependencies.
     
  2. What are npm dependencies?
    NPM dependencies are all the packages used in a project or, in other words, the packages needed for an application to work in production.
     
  3. What are devDependencies?
    devDependencies are the dependencies that are used only in the development stage and not in production.
     
  4. What is the difference between npm dependencies and devDependencies?

npm dependencies

devDependencies

They are used in the projection stage. They are used in the development stage.
Syntax:
npm install <package-name> --save
Syntax:
npm install <package-name> --save-dev

 

5. What is the syntax for npm dependencies and devDependencies?
The syntax for npm dependencies are devDependencies are
npm dependencies:

npm install <package-name--save

 

devDependencies:

npm install <package-name--save-dev

 

Key Takeaways

In this article, we first learned what a dependency is in node.js. We then learned about npm dependencies and devDependencies, where they are used and how to use them. 

I hope you got to learn something new from this article. 

If you want to read more articles like this, be sure to visit the CodingNinjas website.

You may also practice commonly asked coding questions and go through the interview experience of others at CodeStudio.

Happy learning!

Was this article helpful ?
0 upvotes

Comments

No comments yet

Be the first to share what you think