Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] npm run ignores INIT_CWD #3575

Closed
2 tasks done
hinell opened this issue Jul 25, 2021 · 1 comment
Closed
2 tasks done

[BUG] npm run ignores INIT_CWD #3575

hinell opened this issue Jul 25, 2021 · 1 comment
Labels
Bug thing that needs fixing Needs Triage needs review for next steps Release 7.x work is associated with a specific npm 7 release

Comments

@hinell
Copy link

hinell commented Jul 25, 2021

Is there an existing issue for this?

Current Behavior

INIT_CWD is ignored by npm run command.

Expected Behavior

As it follows from the npm run help page (assuming it's still relevant):
[...] 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. [...] [0]

Steps To Reproduce

Run the following in zsh/bash.
What it does:

  1. Creates /tmp/test-npm-cwd subdir with package.json
  2. $ npm run echo from the parental /tmp with INIT_CWD set up to tmp/test-npm-cwd
[[ -d /tmp/test-npm-cwd ]] || mkdir /tmp/test-npm-cwd
cd /tmp/test-npm-cwd
tee package.json <<-EOL
{
        "name": "test-package",
        "scripts": {
                "echo": "echo $INIT_CWD"
        }
}
EOL
cd ..
INIT_CWD="/tmp/test-npm-cwd" npm run echo

The output:

....
npm ERR! enoent ENOENT: no such file or directory, open '/tmp/package.json'

Environment

  • OS: Kubuntu 20.1
  • Node: v16.4.2
  • npm: 7.18.1
@hinell hinell added Bug thing that needs fixing Needs Triage needs review for next steps Release 7.x work is associated with a specific npm 7 release labels Jul 25, 2021
@nlf
Copy link
Contributor

nlf commented Mar 8, 2022

Scripts are run from the root of the package folder, regardless of what the current working directory is when npm run is called. 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.

INIT_CWD isn't meant to allow you to force a directory, it's a read-only variable used so that you can determine what directory the command was run from in your shell. to run an npm script from another directory, you should try npm --prefix /tmp/test-npm-cwd run echo

@nlf nlf closed this as completed Mar 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug thing that needs fixing Needs Triage needs review for next steps Release 7.x work is associated with a specific npm 7 release
Projects
None yet
Development

No branches or pull requests

2 participants