Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to easily verify correct npm dependencies installed?

Tags:

node.js

npm

How can I know when to prompt user to run npm install if there are any unmet package.json dependencies?

I would like to do this, because if any require() fails, the user gets a poor error message:

module.js:340
    throw err;
          ^
Error: Cannot find module 'nopt'

I've previously tried to just check for the existence of a node_modules directory, but this only works effectively for fresh git clones. I've also tried just requiring npm and running npm install as part of load, but that is very heavy weight.

I'm hoping there is a lighter weight library out there that just parses package.json and makes sure node_modules contents satisfy the requirements.

One idea was to use process.on('uncaughtException') to catch only module import errors, but looking to see if there is a "standard" solution first.

like image 284
mmocny Avatar asked Apr 07 '14 14:04

mmocny


People also ask

How do you check if npm is properly installed?

Test NPM. To see if NPM is installed, type npm -v in Terminal. This should print NPM's version number so you'll see something like this 1.4. 28.

How do I see npm package dependencies?

Use the npm list to show the installed packages in the current project as a dependency tree. Use npm list --depth=n to show the dependency tree with a specified depth. Use npm list --prod to show packages in the dependencies . Use npm list --dev to show packages in the devDependencies .


1 Answers

npm ls will report missing packages when run from the project folder.

npm-ls documentation

This might have issues if you're using git dependencies, though. (Thanks @gman).

like image 131
Dylan Avatar answered Sep 30 '22 12:09

Dylan