Jan 9th, ‘25/12 min read

npm Packages: Cheatsheet, Troubleshooting & More

Get the hang of npm with this handy cheatsheet—listing packages, installing, troubleshooting, and tips to make your dev life easier!

npm Packages: Cheatsheet, Troubleshooting & More

In JavaScript development, npm (Node Package Manager) is a go-to tool. Managing dependencies for a personal project or handling multiple packages for a large-scale application, knowing how to list npm packages can save you a lot of time and effort.

This guide takes a closer look at listing npm packages, covering everything from local and global installations to key commands and troubleshooting tips.

npm Packages List Cheatsheet

CommandDescription
npm listList all local packages
npm list --depth=0List top-level local packages (no nested dependencies)
npm list -gList all globally installed packages
npm list -g --depth=0List top-level global packages (no nested dependencies)
npm list <package-name>Check a specific package's version and dependency details
npm list --devList only devDependencies
npm list --jsonOutput the list in JSON format
npm outdatedList outdated packages and their newer versions
npm cache clean --forceClear npm cache to resolve cache-related issues
npm install <package-name>Install a package locally
npm install -g <package-name>Install a package globally
npm uninstall <package-name>Uninstall a local package
npm uninstall -g <package-name>Uninstall a global package
npm outdated --globalList outdated global packages
npm update <package-name>Update a specific package
npm update -g <package-name>Update a global package
npm auditRun security audits on installed packages
npm audit fixAutomatically fix vulnerabilities in installed packages
For more on logging and monitoring, check out our guide on Linux Syslog here.

API and Functionality

API Style/FeatureDescription
Fluent API StyleMethods are chained for cleaner code (e.g., .then(), .catch())
Callback-basedUses callbacks for asynchronous operations (e.g., library.method(params, callback))
Promise-basedUses Promises to handle async code (e.g., .then(), .catch())
Async/awaitSyntactic sugar over Promises for async/await style code
Functional APIStateless, functional approach to APIs (e.g., library.function())
Object-Oriented APIClasses and instances for state and encapsulation (e.g., new LibraryClass())
Extensibility and PluginsAllows extending functionality via plugins (e.g., Express middleware)
Configuration and DefaultsConfigurations with sensible defaults that can be overridden (e.g., library.init({}))

Integration and Compatibility

Integration FeatureDescription
Framework-Specific IntegrationsPackages designed to work with specific frameworks (e.g., React, Express)
Cross-Library CompatibilityLibraries that work across ecosystems (e.g., axios, lodash)
Tree Shaking and Bundler SupportSupport for bundlers like Webpack and Vite, and tree shaking to remove unused code
Version CompatibilityEnsuring compatible versions of dependencies (e.g., using npm dedupe)
Custom Adapters and WrappersWrapping libraries to make them compatible with different frameworks or tools
Polyfills and Backward CompatibilityProviding support for older JavaScript features or browsers

What Are npm Packages?

npm packages are modules or libraries shared by the Node.js community to solve common problems or add functionality to your applications.

These packages are stored in the npm registry and can be installed via the npm CLI (Command Line Interface). From UI frameworks to utility libraries, npm hosts millions of packages.

Why List npm Packages?

Listing npm packages can help you:

  • Track dependencies in your project.
  • Identify outdated packages that need updating.
  • Debug issues by reviewing installed versions.
  • Audit your project for security vulnerabilities.
Learn more about logging in Node.js with our detailed guide on Winston logging.

How to List Installed npm Packages

Here are the different methods to list npm packages:

1. Listing Local Packages

To view all the packages installed in your current project:

npm list

This command displays the dependency tree of your project’s installed packages.

Key Options:

  • --depth=0: Shows only top-level packages.
  • --json: Outputs the result in JSON format.

Example:

npm list --depth=0

Output:

my-app@ 
 ├── express@4.18.2 
 └── mongoose@6.10.0

2. Listing Global Packages

Global packages are installed system-wide and not tied to a specific project. To see them:

npm list -g

Key Options:

  • --depth=0: Prevents displaying nested dependencies.

Example:

npm list -g --depth=0

Output:

/usr/local/lib
 ├── npm@9.6.1
 └── typescript@4.9.5

3. Listing DevDependencies

For projects with separate development dependencies, you can filter them:

npm list --dev

4. Using package.json

Your package.json file contains all your project’s dependencies and devDependencies. You can view them manually or use the npm ls command for automated output.

Check out our post on Morgan in Node.js to learn how it simplifies logging.

Common Issues and Troubleshooting

While listing npm packages, you might encounter some issues. Here are a few tips:

1. Packages Not Showing Up

  • Ensure you’re in the correct directory for local packages or use the -g flag for global packages.

2. Permission Errors

  • Use sudo (on macOS/Linux) or run your command prompt as Administrator (on Windows).

3. Corrupted npm Cache

  • Clear the cache with:
npm cache clean --force

4. Outdated npm

  • Update npm with:
npm install -g npm@latest

API and Functionality: Using npm list

When it comes to npm packages, the API design and available functionalities play a crucial role in shaping the user experience. Let's break it down into some common styles and approaches you might encounter:

1. Fluent API Style

Some npm packages use a fluent interface, where methods are chained together for cleaner and more readable code. For instance:

const result = libraryInstance
  .setOption('key', 'value')
  .enableFeature()
  .execute();

This style simplifies configuration and encourages a natural flow in code writing.

2. Using npm list

The npm list command is part of the npm CLI, allowing you to interact with and understand the structure of your installed packages. Here's how you can use it effectively:

  • Fluent Interaction: While npm list itself isn't exactly a fluent interface, you can think of it as a tool that helps you explore and interact with your dependencies in a smooth, structured way.
  • Package Details: You can list installed packages (both local and global), see dependency trees, and even check versions with commands like npm list <package-name>.

For example, to list all packages in your project:

npm list
Explore how to integrate OpenTelemetry with Express in our guide on OpenTelemetry in Express.

2. Callback vs. Promise-Based APIs

When working with npm packages, you'll encounter different methods for handling asynchronous operations.

Older npm packages might still rely on callbacks, while modern ones often embrace Promises or async/await for better handling of asynchronous tasks.

Let's compare these approaches:

Callback-based:

In this approach, a callback function is passed to handle the result of the asynchronous operation:

library.method(params, (err, result) => {
  if (err) console.error(err);
  else console.log(result);
});

While functional, callback-based APIs can lead to "callback hell" when there are multiple nested callbacks, making the code harder to read and maintain.

Promise-based:

Promises allow you to handle asynchronous results in a more structured and readable manner, chaining .then() and .catch() for success and error handling:

library.method(params)
  .then(result => console.log(result))
  .catch(err => console.error(err));

This approach improves readability by handling asynchronous operations in a linear flow, avoiding deeply nested callbacks.

Async/await:

With async/await, the code becomes even more readable, resembling synchronous code but still handling asynchronous operations. It’s based on Promises and allows for error handling using try/catch blocks:

try {
  const result = await library.method(params);
  console.log(result);
} catch (err) {
  console.error(err);
}

This method is considered the most modern and clean approach for handling asynchronous code, reducing callback complexity and improving code readability.

Check out our comparison of Parquet and CSV formats in the Parquet vs. CSV guide.

3. Functional vs. Object-Oriented

Some packages offer a functional API where functions are standalone and stateless. Others follow an object-oriented design, providing classes and instances to maintain state and encapsulate logic. Here's a comparison:

Functional Example:

In a functional API, the focus is on functions that operate on data without maintaining any internal state:

const result = library.someFunction(input);

This approach is stateless, meaning each function call is independent, making the code simple and predictable.

Object-Oriented Example:

Object-oriented APIs, on the other hand, involve creating instances of classes. These instances can maintain state and encapsulate logic:

const instance = new LibraryClass(config);
const result = instance.someMethod(input);

This approach is useful when you need to manage state or use inheritance and polymorphism, as it encapsulates behavior within objects.

4. Extensibility and Plugins

Many npm packages provide mechanisms to extend their core functionality through plugins or middleware.

For example, web frameworks like Express allow middleware chaining:

app.use((req, res, next) => {
  console.log('Middleware in action');
  next();
});

This allows you to plug in additional functionality or modify the behavior of the core system in a flexible and modular way.

5. Configuration and Defaults

Good APIs strike a balance between sensible defaults and user configurability. Many libraries come with pre-configured defaults to get you started quickly, while also allowing fine-tuned adjustments:

const defaultConfig = library.init(); // Default settings
const customConfig = library.init({ key: 'customValue' }); // Custom settings

This ensures that users can get up and running fast, but also have the flexibility to tweak the settings for more specific needs.

Integration and Compatibility

Integrating npm packages into your project often involves understanding how they interact with other libraries or frameworks.

Compatibility can significantly impact your development process, especially when combining multiple tools.

Here are some key aspects to consider:

1. Framework-Specific Integrations

Many npm packages are designed to work easily with specific frameworks, offering tailored APIs or plugins. For example:

  • React Libraries: Packages like react-router or react-query are built with React in mind, integrating directly into the component lifecycle.
  • Express Middleware: Middleware like cors or body-parser is explicitly crafted to plug into Express applications with minimal setup.

Example:

import { BrowserRouter } from 'react-router-dom';

<BrowserRouter>
  <App />
</BrowserRouter>;

In this case, react-router-dom integrates smoothly into React's component-based structure, providing routing functionality with minimal boilerplate.

You can also explore how to extract account-level CDN metrics from Akamai logs with Last9 in our blog post here.

2. Cross-Library Compatibility

Certain npm packages are designed to bridge the gap between different ecosystems. For instance, tools like axios or lodash can integrate into virtually any JavaScript project due to their universal design and lack of reliance on specific frameworks.

Example:

import axios from 'axios';

axios.get('/api/data')
  .then(response => console.log(response.data));

In this case, axios is a versatile HTTP client that works across different JavaScript environments, making it a reliable choice for API calls regardless of the framework or library in use.

3. Tree Shaking and Bundler Support

Modern JavaScript tools prioritize tree shaking to optimize bundle size by removing unused code. When integrating npm packages, it's crucial to check their support for modern module systems like ES Modules (ESM) or CommonJS.

For instance, some older packages might need transpilation to work with bundlers like Webpack or Vite, while newer ones often provide dual support out of the box. This ensures that only the necessary parts of the package are bundled into your final output, keeping your project lean and efficient.

4. Version Compatibility

Dependency conflicts can arise when two packages require different versions of the same library. To avoid this, always verify peer dependencies and check for compatibility notes in the documentation.

Tools like npm dedupe or yarn resolutions can help manage conflicts by ensuring that only one version of a dependency is installed.

5. Custom Adapters and Wrappers

For npm packages that aren't natively compatible with a framework or tool, developers often write custom adapters. These wrappers translate the package's API into a format that aligns with your project's needs.

Example:

Wrapping a vanilla JavaScript charting library for React components:

import Chart from 'chart-library';

const ChartWrapper = ({ data, options }) => {
  useEffect(() => {
    const chart = new Chart('chart-container', data, options);
    return () => chart.destroy();
  }, [data, options]);

  return <div id="chart-container"></div>;
};

In this example, the ChartWrapper component allows a non-React charting library to be used within a React application, ensuring that it fits into the component-based lifecycle of React.

6. Polyfills and Backward Compatibility

If you're working in an environment that doesn't support the latest JavaScript features, you might need polyfills. Polyfills are scripts that add missing features to older environments, enabling them to work as though they support modern features.

Some npm packages provide built-in support for older browsers, ensuring that functionality works without the need for additional libraries. Others, however, might rely on external polyfill libraries to provide compatibility.

For example, if you're using newer ECMAScript features like Promise or fetch in an older browser, you can include a polyfill package to ensure that the features work correctly:

import 'core-js/stable';
import 'regenerator-runtime/runtime';

This ensures that even users with older browsers can experience your application as intended, without running into issues caused by missing JavaScript features.

For more on monitoring your servers effectively, check out our guide on server monitoring tools.

npm Package Ranking and Metrics

Choosing the right npm package often involves evaluating its ranking and metrics. These indicators can reveal a package's reliability, popularity, and standing in the ecosystem. Here's a closer look at what to consider:

1. Download Statistics

The number of downloads a package receives is a quick way to gauge its popularity. Regularly updated packages with millions of weekly downloads, like lodash or axios, are typically well-regarded and widely used.

To check download stats, you can use tools like npm trends:

npm install -g npm-trends
npm trends package-name

2. Stars and Forks

GitHub stars and forks are indicators of community interest and engagement. A high star count usually suggests the package has a solid reputation, while forks might signal active development and customization.

3. Dependency Count

Fewer dependencies often mean a smaller attack surface and reduced risk of dependency conflicts. Tools like Bundlephobia can help analyze a package's size and dependency footprint:

npx bundlephobia package-name

4. Last Updated

A package's maintenance activity is critical. Regular updates indicate an actively supported project, while a stale package might lead to compatibility issues or security vulnerabilities.

Check the "last updated" date on npm or GitHub to ensure the package aligns with modern development standards.

5. License and Security

A package's license affects its usage in commercial or open-source projects. Additionally, security audits for vulnerabilities in dependencies can prevent potential risks. Tools like npm audit can flag security issues:

npm audit
Looking for log aggregation solutions? Explore our post on log aggregation tools for more insights.

6. Community Support

Active community involvement, such as issue resolution and discussions on GitHub, demonstrates a healthy ecosystem around a package. Packages with responsive maintainers and an engaged user base tend to evolve faster and are often better supported. Check for open issues, pull requests, and the frequency of updates to gauge how actively the package is maintained.

7. Ecosystem Authority

Some packages are considered standards within their domain, with widespread use and a robust ecosystem of plugins and extensions. For example:

  • UI Frameworks: React, Vue, Angular
  • State Management: Redux, MobX, Zustand
  • HTTP Clients: axios, fetch, node-fetch

These packages dominate their niche and are often backed by strong community and organizational support, making them reliable choices.

8. Custom Metrics

Developers might also look at custom metrics, such as:

  • Test Coverage: The percentage of code covered by tests, which can indicate stability.
  • Benchmark Performance: How well the package performs in various scenarios.
  • Compliance with Requirements: For example, TypeScript support or compatibility with certain environments (e.g., serverless functions).

These metrics help in evaluating whether a package will meet your specific needs, especially for performance-sensitive or large-scale projects.

Want to understand event logs in Windows? Check out our detailed guide on event logs in Windows.

Putting Metrics to Use

Here’s a simple checklist for evaluating an npm package:

  1. Popularity: Check download stats and GitHub stars.
  2. Reputation: Look for mentions in blogs, tutorials, or community forums.
  3. Activity: Ensure the package is actively maintained and updated.
  4. Compatibility: Verify compatibility with your tech stack.
  5. Security: Audit the package and its dependencies for vulnerabilities.

Conclusion

Listing npm packages is a key skill for every developer. From debugging and auditing to exploring your dependencies, the commands we’ve covered will help you stay on top of your projects and keep things running smoothly.

If you want to chat more, our community on Discord is always open. We have a dedicated channel where you can connect with other developers and discuss your specific use case.

FAQs

How do I list all packages in npm?

You can browse all npm packages on the official npm registry. Use npm search to find packages based on keywords like parse, routing, or templates:

npm search <keyword>

How many npm packages are there?
npm hosts over 2 million packages, including tools for node.js applications, real-time communication, and web applications. Updated numbers can be found on the npm website.

What are the most popular npm packages?
Some of the most popular npm packages include:

  • lodash: A widely-used JavaScript library for utility functions.
  • axios: A robust HTTP client with real-time response handling.
  • socket.io: A popular tool for real-time communication in node.js applications.
  • mongodb: A driver for interacting with MongoDB databases in node.js development.

Where are all npm packages stored?
npm packages are stored in the npm registry, and locally in your project's node_modules folder or globally in your system's npm directory. Use npm docs for detailed schema and validation information.

How do I list all the dependencies in npm?
List all project dependencies using:

npm list --depth=0

To include detailed parse and schema information, omit the --depth=0 flag.

How to list installed npm packages?
Local packages:

npm list --depth=0

Global packages:

npm list -g --depth=0

How to install npm fs in Node.js?
The fs module is part of the node.js runtime and doesn't require installation. Use it for file operations:

const fs = require('fs'); 
fs.readFile('file.txt', 'utf8', (err, data) => console.log(data));

How do you load globally installed packages?
Globally installed packages like nodemailer or mocha are available in the CLI. Example:

nodemailer --help

How to install a local module using npm?
Install a local npm package, such as one built for node.js development:

npm install /path/to/local-module

What are the best tools for API deployment?
Some top tools for API deployment include:

  • Postman: For testing and validation.
  • Swagger: For API documentation and workflow design.
  • AWS API Gateway: By Amazon, for deploying serverless APIs.

How do I pass command line arguments to a Node.js program and receive them?
Use process.argv to parse arguments:

node script.js arg1 arg2
console.log(process.argv);

How can I view a list of all globally installed npm packages?
Run the following:

npm list -g --depth=0

How can I view a list of all npm packages I’ve installed?
To view globally installed packages:

npm list -g

To see local node.js applications dependencies:

npm list --depth=0

How can I view all installed npm packages in my project?
In your project directory, list dependencies:

npm list --depth=0

How to uninstall npm packages?
To uninstall a package (e.g., socket.io) from a project:

npm uninstall socket.io

How to optimize npm workflows?
Simplify node.js development with tools like nodemailer, mocha, or real-time communication libraries like socket.io. Use npm dedupe to reduce dependency bloat.

Contents


Newsletter

Stay updated on the latest from Last9.

Authors

Preeti Dewani

Technical Product Manager at Last9

Handcrafted Related Posts