Displaying locally installed npm packages in current project

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();

Thanks in advance for your suggestions!

yo, try ‘npm list --parseable --depth=0’ in ur project folder. it’ll give ya a clean list of just the packages u installed directly. no extra fluff.

if u wanna get fancy, u can pipe it to ‘sed’ to strip out the full path:

npm list --parseable --depth=0 | sed ‘s/.*node_modules\///’

works like a charm for me. hope it helps!

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:

const { execSync } = require('child_process');

function showLocalPackages() {
  const output = execSync('npm list --depth=0 | grep -v \'npm@\'', { encoding: 'utf-8' });
  console.log('Local project packages:');
  console.log(output);
}

showLocalPackages();

Hope this helps you streamline your workflow!

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:

const { execSync } = require('child_process');

function showLocalPackages() {
  try {
    const output = execSync('npm ls --depth=0', { encoding: 'utf-8' });
    console.log('Local project packages:');
    console.log(output);
  } catch (error) {
    console.error('Error listing packages:', error.message);
  }
}

showLocalPackages();

This approach is straightforward and doesn’t require any additional tools or grep commands.