Node.js Dependencies: The BFFs Your Code Can't Live Without

Photo by RetroSupply on Unsplash

Node.js Dependencies: The BFFs Your Code Can't Live Without

The Superheroes Behind Your Application's Success

Different Dependencies present in Node.js Application

/

When working on a project with Node.js, ever wondered when we try to install packages, our package.json file is flooded with some JSON data describing the packages which we have installed. Let's try to dig deep into it and find out what all different dependencies are present in our project.

Keeping track of dependencies is a important task when we are working on a huge project. We might have those dependencies serving different purposes to our project.

Also, when we are at different stages of our project let us say in development stage or at production stage, the way these dependencies work will change. So, lets start out dive. I'll try to explain them in a simple manner and yet good way to remember them while working with them in a project. Happy Learning!!

What exactly is a Dependency?

  • Dependencies in Node.js are external modules or packages that are required for an application to function correctly.

  • These packages can include libraries, frameworks, utilities, and other code modules that provide specific functionality or features.

  • Whenever we install a package, they are recorded as dependency in our package.json file

  • Different dependencies describe different use cases in our project. They are

Core Dependencies:

  • Node.js has a set of core modules that are bundled with the runtime and are readily available for use.

  • These modules provide fundamental functionality such as file system operations, networking, and data stream handling.

  • They do not require installation or management through a package manager since they are already built into Node.js.

  • Example, the http module provides the foundation for creating web servers, while the fs module facilitates file system operations.

      const http = require('http');
    

NPM (Node Package Manager) Dependencies:

  • providing a vast repository of packages that can be easily installed and managed within your projects.

  • NPM dependencies are defined in a project's package.json file and can be categorized into two types: Production dependencies and Development dependencies.

    1. Production Dependencies:

      Production dependencies are essential for the proper functioning of your application in a production environment. These dependencies include frameworks, libraries, and utilities that your application relies on.

       npm install express
      
    2. Development Dependencies:

      Development dependencies, on the other hand, are only required during the development process. They include tools, testing frameworks, and other packages that aid development but are not necessary for the runtime execution of your application.

      They are stored as dev-dependencies in pacakge.json file

       npm install --save-dev mocha
      
    3. Local Dependencies:

      They refer to custom modules or packages that you create within your project. These dependencies can be shared across multiple files within your application, promoting code reusability and maintainability.

      They are usually stored in a subdirectory, such as ./node_modules, within your project directory.

       const myModule = require('myModule');
      
    4. Scripts:

      • The "start" script executes the index.js file using the node command when you run npm start.

      • The "test" script runs Mocha tests when you run npm test.

Here is an example package.json file describing all the dependencies -

    {
      "name": "my-node-project",
      "version": "1.0.0",
      "description": "Demo package.json file for Node.js dependencies",
      "author": "Your Name",
      "license": "MIT",
      "dependencies": {
        "express": "^4.17.1",
        "lodash": "^4.17.21"
      },
      "devDependencies": {
        "mocha": "^9.0.0",
        "nodemon": "^2.0.12"
      },
      "scripts": {
        "start": "node index.js",
        "test": "mocha"
      }
    }

Efficiently managing dependencies involves regularly updating and reviewing them to ensure compatibility, security, and performance. By staying up to date with the latest versions and resolving any conflicts or vulnerabilities, you can ensure the smooth functioning of your application.

Hope you got something useful from the blog!! Do subscribe for the news letter and follow me on Twitter!! Thank you!!