What's the best way to see all my npm-linked Node.js modules?

Hey folks, I need some help with npm and Node.js!

I’m trying to figure out how to get a list of all the global modules I’ve installed, especially the ones I’ve linked using npm link. It would be super helpful if I could see:

  1. The names of all globally installed modules
  2. Which ones are linked (maybe with a special mark or something)
  3. The local paths for the linked modules

Is there a command or trick to do this? I’ve been digging through npm docs but can’t seem to find exactly what I’m looking for. Any ideas?

Here’s a simple example of what I’m hoping to see:

function listModules() {
  const modules = {
    express: { linked: true, path: '/home/user/projects/express' },
    lodash: { linked: false },
    myCustomModule: { linked: true, path: '/home/user/projects/myCustomModule' }
  };
  
  console.log('Installed modules:');
  for (const [name, info] of Object.entries(modules)) {
    console.log(`- ${name}${info.linked ? ' (linked)' : ''}`);
    if (info.linked) console.log(`  Path: ${info.path}`);
  }
}

listModules();

Any help would be awesome. Thanks!

yo, have u tried the npm ls -g --depth=0 command? it shows global packages. for linked ones, maybe check npm ls -g --link. not sure if it gives exact paths tho. might need to combine these with some bash magic to get everything u want. good luck!

Hey there! I’ve actually dealt with this issue before and found that combining a few npm commands with a bit of Node.js scripting yielded the best results. Instead of a single command, you can first run npm ls -g --depth=0 --json to get the JSON output of your global packages. Then, use npm ls -g --link --json to capture information about linked modules. By writing a small Node.js script, you can merge the outputs, check for linked status, and even resolve their actual paths. Although this method may be slightly slower if you have many packages, it ultimately provides a thorough view of your module setup.

I’ve encountered this issue before, and here’s a solution that worked for me. You can use a combination of npm commands and Node.js to achieve what you’re looking for. First, run npm ls -g --depth=0 --json to get a list of global packages. Then, use npm ls -g --link --json for linked modules. Finally, create a simple Node.js script to process this data, combining the outputs and resolving paths for linked modules. This approach gives you a comprehensive view of your setup, including names, linked status, and local paths. It’s a bit more involved than a single command, but it provides the detailed information you’re after. Let me know if you need help with the script specifics.