DEV Community

ServBay
ServBay

Posted on

How to Use ServBay to Enable npm

npm (Node Package Manager) is Node.js's package management tool and its default package manager. It is used to install, share, and manage JavaScript packages and is one of the largest open-source libraries globally. Using npm can help developers easily manage project dependencies and enhance development efficiency.

Download ServBay

Image description

Enabling npm

Node.js installed through ServBay comes with npm enabled by default. If you find that npm is not enabled or needs updating, you can follow these steps.

Confirming npm Installation

Open the terminal and enter the following command to check the npm version number

npm -v
Enter fullscreen mode Exit fullscreen mode

Example output

9.1.0
Enter fullscreen mode Exit fullscreen mode

Updating npm

If you need to update npm, you can update it with the following command

npm install -g npm
Enter fullscreen mode Exit fullscreen mode

Confirm the version number again

npm -v
Enter fullscreen mode Exit fullscreen mode

Benefits of Using npm

The main advantage of npm is its globally largest open-source library and convenient package management capabilities. Here are some practical examples of using npm

Initializing a Project

Use npm to initialize a new Node.js project

npm init
Enter fullscreen mode Exit fullscreen mode

This will guide you to create a new package.json file containing basic information and dependencies of the project.

Installing Dependencies

Use npm to install project dependencies

npm install
Enter fullscreen mode Exit fullscreen mode

This will install all dependencies based on the package.json file.

Adding a Dependency

Add a new dependency package

npm install lodash --save
Enter fullscreen mode Exit fullscreen mode

This will install the lodash package and update the package.json file.

Removing a Dependency

Remove a dependency package

npm uninstall lodash --save
Enter fullscreen mode Exit fullscreen mode

This will remove the lodash package from the project and update the package.json file.

Updating Dependencies

Update all dependencies in the project

npm update
Enter fullscreen mode Exit fullscreen mode

Using npm Scripts

npm allows defining scripts in the package.json file, making it convenient to execute common commands. For example, add the following scripts to the package.json file

"scripts": {
  "start": "node app.js",
  "test": "mocha"
}
Enter fullscreen mode Exit fullscreen mode

Then you can run these scripts with the following commands

npm start
npm test
Enter fullscreen mode Exit fullscreen mode

Common Commands

Install a Global Package

npm install -g <package-name>
Enter fullscreen mode Exit fullscreen mode

For example, install nodemon

npm install -g nodemon
Enter fullscreen mode Exit fullscreen mode

View Global Packages

npm list -g --depth=0
Enter fullscreen mode Exit fullscreen mode
npm cache clean --force
Enter fullscreen mode Exit fullscreen mode

By using npm, developers can easily manage project dependencies, quickly install and update packages, and thus improve overall development efficiency.

Big thanks for sticking with ServBay. Your support means the world to us 💙.
Got questions or need a hand? Our tech support team is just a shout away. Here's to making web development fun and fabulous! 🥳
If you want to get the latest information, follow X(Twitter) and Facebook.
If you have any questions, our staff would be pleased to help, just join our Discord community

Top comments (0)