What is the method to view npm packages installed by the user?

I want to find a way to specifically list only the packages that the user has installed in npm. When I run npm -g list, it shows all packages along with their dependencies, which is too broad. Instead, I need to see just the packages that are installed in the current working directory or environment.

To list only the npm packages that you've specifically installed in the current working directory (without the dependencies), you can use the command npm list --depth=0. This command provides a concise view by limiting the tree depth to zero, showing only the top-level packages.

Here's how you can use it:

npm list --depth=0

This will output the list of packages in your current project directory without diving into their dependencies, making it easier to focus on the user-installed packages.

For global packages, if you want a similar result, you can use:

npm list -g --depth=0

This keeps your view tidy and saves time by directly providing the information you need.

While npm list --depth=0 is indeed a great way to narrow down your view to the top-level npm packages in your current project, you might sometimes want to double-check which packages were manually installed by your team or yourself, especially if you are maintaining a large project. This can be crucial for auditing purposes or just for maintaining clean project dependencies.

In addition to npm list, consider leveraging the project's package.json file. Here, you can specifically look at the "dependencies" and "devDependencies" sections to see what was directly installed:

{
  "dependencies": {
    "express": "^4.17.1",
    "lodash": "^4.17.21"
  },
  "devDependencies": {
    "mocha": "^8.3.2",
    "chai": "^4.3.4"
  }
}

In the above snippet, packages like express and lodash are directly installed into the project as dependencies, and you can find the same for development dependencies like mocha and chai. Checking package.json is a straightforward way to audit what has been specifically included by the user.

This may not list installed versions that have been updated, but it serves as a starting point for tracking user-intended installations.