NPM (Node Package Manager)

While node is the tool you use to run your code, NPM (Node Package Manager) is the tool you use to manage your code’s dependencies.

NPM is the world’s largest software registry, hosting over 1.3 million open-source code packages. It is the JavaScript equivalent to Python’s pip or Go’s go get.

Initialization: The package.json

When starting a new JavaScript project, the first step is to initialize NPM. Running this command in your terminal creates a package.json file:

npm init -y

The package.json file is the heart of any Node project. It acts as a configuration file that tells Node and NPM about your project’s metadata, dependencies, and custom scripts.

Dependencies

When you need to use an external library (like express for web servers, or jest for testing), you install it via NPM:

npm install express

NPM will download the code into a local node_modules folder and automatically update your package.json to keep track of the dependency.

NPM Scripts

The scripts block inside the package.json is incredibly powerful. It allows you to define custom terminal commands to run complex tasks.

{
  "name": "my-project",
  "version": "1.0.0",
  "scripts": {
    "start": "node main.js",
    "test": "jest"
  }
}

Instead of typing out long, complex Node commands manually, you simply run npm run start or npm run test in your terminal.