简体   繁体   中英

How to refer global module for a nodejs script? Can I refer globally installed modules in my nodejs script?

is there a way to use the globally installed moduled directly without declaring package.json and/or installing the modules in the current directory? ie Just like a batch/shell job, running a small script with just a file itself..

Let me give you the context, in my job I keep getting various request like currently I am processing some file manually, I usually write script for these tasks comprising for few 10 to 20 lines to make things easy for me and save few hours. Though the script is probably never be gonna used again. To manage the dependent module, I either declare them in package.json or install locally one by one by using npm install <package-name> . It will be very handy if I can achieve it with a single js file like a shell/batch script.

I guess this would be possible if somehow my script is able to refer a global modules.

You can use npm link in your project folder without installing the module locally.

npm link <module>

Alternatively you can also use NODE_PATH env variable to tell your scripts where your globally installed packages are, this way accessing them as they were local.

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