npm Overview and Key Concepts

AnuraG Singh - Jul 8 - - Dev Community

npm, short for Node Package Manager, is a package manager for JavaScript and the default package manager for the Node.js runtime environment. It helps developers manage dependencies in their projects and provides a vast registry of reusable code packages.
Basically it is a configuration for npm

Key Features of npm

  1. Package Management:
  • Install, update, and uninstall packages (libraries or modules).
  • Manage dependencies required for your project.
  1. Versioning:
  • Specify the version of a package to ensure compatibility and stability.
  1. Script Running:
  • Define and run custom scripts for various development tasks, such as
    testing, building, and deploying, through the scripts section in
    package.json.

    • Custom script can be run using npm run <script-name>
  1. Dependency Management:
  • Automatically handle dependency conflicts and ensure that the correct
    versions of dependencies are used.

  • Different types of dependencies can be specified in the package.json
    file, such as regular dependencies, devDependencies, peerDependencies,
    and optionalDependencies.

Package-lock.json

  • It keeps the track of all the dependency's version in hash form(SHA)

  • The package-lock.json file ensures consistency across different environments by locking the exact versions of all installed dependencies. It helps avoid dependency conflicts and ensures that the correct versions of dependencies are used. Basically avoids version mis-match.

for eg your project is buid using react 18 and if the project is setup on any other pc with older version of react pre-installed let say react 17 then it will hampers the proper functioning of the code or your project , Here package-lock.json comes into role i.e when ever you deploy you project into any other environment it will instantly install the react 18 regardless of pre-installed react 17

package.json Code

{
  "name": "my-project", //app or project name
  "version": "1.0.0", // apps version 
  "description": "A sample project",
  "main": "index.js", 
  "scripts": {
    "start": "node index.js", // npm run start will run index.js
    "test": "echo \"Error: no test specified\" && exit 1", // use jest
    "build": "webpack --config webpack.config.js",
    "lint": "eslint .", //catches error
    "deploy": "npm run build && firebase deploy" //builds and deploy onto //firebase
  },
  "dependencies": {
    "express": "^4.17.1",
    "react" : "^18.0"
  },
  "devDependencies": {
    "eslint": "^7.11.0",
    "webpack": "^4.44.2"
  }
}

Enter fullscreen mode Exit fullscreen mode
. . .