npm - Useful Commands

ryoichihomma

Ryoichi Homma

Posted on September 21, 2024

npm - Useful Commands

Concept Highlights:

  1. npm outdated
  2. npm cache verify
  3. npm cache clean --force
  4. Installing socket.io
  5. npm audit
  6. Using nodemon
  7. Installing Babel with --save-dev

1. npm outdated

When working on a project, your dependencies may become outdated as new versions of packages are released. The npm outdated command is a great way to check for any packages that are behind on updates.

This command will display a table listing the current version, the latest version, and the version your project depends on for each outdated package.

Example Outputs:

Package     Current  Wanted  Latest  Location
express      4.17.1   4.17.3  4.18.1  node_modules/express
Enter fullscreen mode Exit fullscreen mode
  • Current: Version installed.
  • Wanted: The version your package.json specifies.
  • Latest: The newest version available.

If packages are outdated, you can run npm update to install the latest versions.

2. npm cache verify

npm caches packages to speed up future installs, but sometimes the cache can become corrupted. You can check the integrity of the npm cache using npm cache verify.

This command checks the cache for any issues and ensures everything is functioning correctly. If there are any discrepancies, npm will try to fix them. Regular cache verification helps keep your Node.js environment stable.

3. npm cache clean --force

In cases where npm cache verify finds issues or you need to clear the cache, you can use the npm cache clean --force command. This will remove the npm cache entirely.

Note that this should be used with caution since clearing the cache could slow down future installs as npm will need to redownload packages.

4. Installing socket.io

One of the powerful tools for real-time communication in web apps is Socket.IO. It is used to enable WebSocket communication between clients and servers. To install it in your project, run:

npm install socket.io
Enter fullscreen mode Exit fullscreen mode

This adds Socket.IO to your node_modules and saves it as a dependency in your package.json file. Now you can use it in your project to handle real-time events like chats, notifications, and more.

5. npm audit

Security is a top priority, and npm has a built-in tool to help identify and fix security vulnerabilities in your project’s dependencies. The npm audit command scans the installed packages for known vulnerabilities and provides guidance on how to address them.

If any vulnerabilities are found, you’ll get a report detailing the severity and recommendations on how to fix them, such as running npm audit fix to automatically update insecure dependencies.

6. Using nodemon

During development, you often need to restart your Node.js server whenever you make changes to your code. Nodemon is a helpful tool that automatically restarts the server for you, making development faster and more efficient.

Install it globally with:

npm install -g nodemon
Enter fullscreen mode Exit fullscreen mode

Or locally to your project:

npm install --save-dev nodemon
Enter fullscreen mode Exit fullscreen mode

Then, instead of running node app.js, you can run your app using:

nodemon app.js
Enter fullscreen mode Exit fullscreen mode

This will watch for any changes in your files and automatically restart the server when it detects changes.

7. Installing Babel with --save-dev

When working with modern JavaScript (ES6+) in Node.js projects, you may need to transpile code to ensure compatibility with older environments. Babel is a powerful JavaScript compiler, and installing it as a dev dependency ensures that it’s only used during development and not in production.

To install Babel CLI as a dev dependency, run:

npm install --save-dev babel-cli
Enter fullscreen mode Exit fullscreen mode

This command adds Babel to your project without adding it to the production environment, making your app lighter and more efficient.

e.g.)
You’ll typically use Babel with a .babelrc file to configure presets and plugins. Once set up, you can use Babel to transpile your code with commands like:

npx babel src --out-dir dist
Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
ryoichihomma
Ryoichi Homma

Posted on September 21, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

npm - Useful Commands
npm npm - Useful Commands

September 21, 2024