Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can NPM scripts use my current working directory (when in nested subfolder)

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.

like image 434
lxyyz Avatar asked Sep 13 '17 19:09

lxyyz


People also ask

In which directory should you place the NPM scripts?

then you could run npm start to execute the bar script, which is exported into the node_modules/. bin directory on npm install .

How can I get NPM start at a different directory?

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 ."

What is Init_cwd?

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.


2 Answers

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.

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

like image 71
Marcel Stör Avatar answered Sep 18 '22 06:09

Marcel Stör


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?

like image 35
lxyyz Avatar answered Sep 19 '22 06:09

lxyyz