简体   繁体   中英

How to know if a module is installed in node

I know that I can install a module in node.js using npm install module-name . but try that multiiple times and it will install again. I am tired of forgetting that I have installed modules globally and I am installing them again. How do I know if a module installation already exists globally in node?

you have to try

npm ls

will give you the list of installed modules

to list global packages

npm ls -g

to list global packages with more detail

npm ls -gl

same way to list local packages with detail

npm ls -l

also you can type

npm help ls

for more details regarding this

If you want to know whether you have a specific module installed, you can run npm explore <module> . If you get an error, then you don't have that module and you should download it.

This is a bit of a hack, but it works.

in your js code, just do this:

try {
    console.log(require.resolve("some-npm-module-name"));
} catch(e) {
    console.error('moduel is not installed');
    process.exit(e.code);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM