It's good that I can run NPM scripts not only from the project root but also from the subfolders. However, with constraint that it can't tell my current working path ($PWD).
Let's say there's a command like this:
"scripts": {
...
"pwd": "echo $PWD"
}
If I run npm run pwd
within a subfolder of the project root (e.g, $PROJECT_ROOT/src/nested/dir
), instead of printing out my current path $PROJECT_ROOT/src/nested/dir
, it always gives $PROJECT_ROOT
back. Are there any way to tell NPM scripts to use my current working directory instead of resolving to where package.json resides?
Basically I want to pull a Yeoman generator into an existing project and use it through NPM scripts so that everyone can use the shared knowledge (e.g, npm run generator
) instead of learning anything Yeoman specific (e.g npm i yo -g; yo generator
). As the generator generates files based on current working path, while NPM scripts always resolves to the project root, I can't use the generator where it intend to be used.
then you could run npm start to execute the bar script, which is exported into the node_modules/. bin directory on npm install .
Run the command in a different folder # You can use the option --prefix <path/to/folder> to run NPM command in a particular folder. It works like a cross-platform cd <path/to/folder>; npm ... combination. "start": "node ."
If you want your script to use different behavior based on what subdirectory you're in, you can use the INIT_CWD environment variable, which holds the full path you were in when you ran npm run . npm run sets the NODE environment variable to the node executable with which npm is executed.
If you want your script to use different behavior based on what subdirectory you’re in, you can use the
INIT_CWD
environment variable, which holds the full path you were in when you rannpm run
.
Source: https://docs.npmjs.com/cli/run-script
Use it like so:
"scripts": {
"start": "live-server $INIT_CWD/somedir --port=8080 --no-browser"
}
Update 2019-11-19
$INIT_CWD
only works on *nix-like platforms. Windows would need %INIT_CWD%
. Kind of disappointing that Node.js doesn't abstract this for us. Solution: use cross-env-shell live-server $INIT_CWD/somedir....
-> https://www.npmjs.com/package/cross-env
One known solution is through ENV variable injection.
For example:
Define scripts in package.json:
"pwd": "cd $VAR && echo $PWD"
Call it from anywhere sub directories:
VAR=$(pwd) npm run pwd
However, this looks really ugly, are there any cleaner/better solutions?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With