JavaScript Q&A Logo
JavaScript Q&A Part of the Q&A Network

How do I manage dependencies in a Node.js project with JavaScript?

Asked on Sep 20, 2024

Answer

In a Node.js project, dependencies are managed using the "package.json" file and the Node Package Manager (NPM). This file lists all the dependencies your project requires.
<!-- BEGIN COPY / PASTE -->
        {
          "name": "my-node-project",
          "version": "1.0.0",
          "description": "A simple Node.js project",
          "main": "index.js",
          "scripts": {
            "start": "node index.js"
          },
          "dependencies": {
            "express": "^4.17.1"
          },
          "devDependencies": {
            "nodemon": "^2.0.7"
          }
        }
        <!-- END COPY / PASTE -->
Additional Comment:
  • The "dependencies" section lists packages required for your application to run.
  • The "devDependencies" section lists packages needed only during development.
  • Use the command "npm install" to install all listed dependencies.
  • Use "npm install package-name" to add a new dependency and automatically update "package.json".
  • Use "npm install package-name --save-dev" to add a package to "devDependencies".
✅ Answered with JavaScript best practices.
← Back to All Questions