Hey everyone, I’m trying to figure out how to see just the packages I’ve installed for this project without the extra dependencies from global installs. When I run npm -g list, it shows every package along with all its dependencies, which is more than what I need. I’m looking for a command or flag that lists only the packages I explicitly added. I’ve scoured the npm documentation but haven’t found the right solution yet. Could someone help me out?
Below is a sample function demonstrating what I’d like to achieve:
function showLocalPackages() {
// Code to list only the packages installed in the current project
console.log('Local project packages:');
// Expected output is a clean list of directly installed packages
}
showLocalPackages();
I’ve been in a similar situation, and I found a neat trick that might help you out. Instead of using npm -g list, try running npm list --depth=0 in your project directory. This command will show you only the top-level packages you’ve installed for your project, without all the nested dependencies.
If you want to take it a step further, you can combine it with grep to filter out any extraneous info:
npm list --depth=0 | grep -v 'npm@'
This will give you a clean list of just your project’s direct dependencies. It’s been a lifesaver for me when I need to quickly check what I’ve installed or share my project setup with teammates.
As for your function, you could implement it like this:
I’ve found a simple solution that works well for me. Just run npm ls --depth=0 in your project directory. This command lists only the top-level packages you’ve installed, without all the nested dependencies.
If you want to exclude devDependencies, you can add the --production flag: npm ls --depth=0 --production
For your showLocalPackages function, you could implement it like this: