简体   繁体   中英

get source folder when executing globally installed npm cli package

I am creating my own CLI using NodeJS. One task of the CLI is to copy folders recursively from a source folder within the project.

So, I install my CLI globally using npm install -g from within my CLI project folder. Now I can run it in another test folder. However, when I run it, the recursive copy fails because it is trying to find the files from the source folder in the test folder I created. Not from /usr/local/lib/node_modules/[myCli] .

I have tried many solutions using path and require.resolve and __filename but they all give me a folder starting from the test folder in which I am executing my CLI.

Other then hardcoding the source path to /usr/local/lib.... , there most be a way to get the folder of the globally executed script?

If you search for the current working directory its process.cwd() https://nodejs.org/api/process.html#process_process_cwd

__filename give you only path of file where it installed in node_modules

To find your project files you can use rc file in your home directory containing the path of your project https://en.wikipedia.org/wiki/Run_commands

in hope this helps

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