Open In App

How to configure NPM ?

Last Updated : 01 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Node Package Manager (npm) is a powerful tool that streamlines package management and task automation in Node.js and JavaScript projects. Proper configuration of npm is crucial for optimizing workflows and ensuring consistent development experiences.

This guide will walk you through the process of configuring npm, from setting global preferences to fine-tuning project-specific settings, enabling you to maximize productivity and efficiency.

Prerequisites:

Implementation:

Step 1: Installing npm If you haven’t already installed npm, you can do so by installing Node.js, which comes bundled with npm.

For Linux:

# Install Node.js (which includes npm)
sudo apt install nodejs

For Windows:

Visit the official Node.js website (https://nodejs.org/en) and download the latest version for Windows. Run the installer and follow the installation instructions. npm will be installed automatically along with Node.js.

Checking npm Version:

npm --version
Screenshot-2024-03-25-001000

output

2. Configuring Global Preferences

To set a global configuration, you use the npm config set command with the –global flag.

# Example: Set the default registry globally
npm config set registry https://registry.npmjs.org/ --global

3. Fine-Tuning Per-Project Settings:

To set a local configuration, you use the npm config set command without the –global flag. This command should be run in the root directory of your project.

# Example: Set the default package version locally for a project
npm config set init.version 1.0.0

4. Using npm Scripts:

You can simplify repetitive tasks and seamlessly integrate npm commands into your project’s workflow by leveraging npm scripts.

package.json:

{
"scripts": {
"build": "webpack --mode=production",
"start": "webpack-dev-server --mode=development"
}
}

You can run these scripts using npm run <script-name>.

npm run build

Output:

run-buld-op


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads