-
Notifications
You must be signed in to change notification settings - Fork 4k
docs: Clarify script working directory behavior (fixes #8305) #8308
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
Merged
owlstronaut
merged 6 commits into
npm:latest
from
tarekwfa0110:docs/8305/clarify-script-working-directory
May 21, 2025
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
369eb67
docs: Clarify script working directory behavior (fixes #8305)
tarekwfa0110 85a3582
Revert "docs: Clarify script working directory behavior (fixes #8305)"
tarekwfa0110 45c12d7
docs: Clarify script working directory behavior (fixes #8305)
tarekwfa0110 4e99636
Update scripts.md
tarekwfa0110 455cb24
Update scripts.md with missing backticks
tarekwfa0110 103fb78
Update scripts.md with missing backticks 2
tarekwfa0110 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -228,6 +228,20 @@ Reasons for a package removal include: | |||
|
|
||||
| Due to the lack of necessary context, `uninstall` lifecycle scripts are not implemented and will not function. | ||||
|
|
||||
| ### Working Directory for Scripts | ||||
|
|
||||
| Scripts are always run from the root of the package folder, regardless of what the current working directory is when npm is invoked. This means your scripts can reliably assume they are running in the package root. | ||||
|
|
||||
| If you want your script to behave differently based on the directory you were in when you ran npm, you can use the INIT_CWD environment variable, which holds the full path you were in when you ran npm run. | ||||
tarekwfa0110 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||
|
|
||||
| #### Historical Behavior in Older npm Versions | ||||
|
|
||||
| For npm v6 and earlier, scripts were generally run from the root of the package, but there were rare cases and bugs in older versions where this was not guaranteed. If your package must support very old npm versions, you may wish to add a safeguard in your scripts (for example, by checking process.cwd()). | ||||
|
|
||||
| For more details, see: | ||||
| - [npm v7 release notes](https://github.com/npm/cli/releases/tag/v7.0.0) | ||||
| - [Discussion about script working directory reliability in npm v6 and earlier](https://github.com/npm/npm/issues/12356) | ||||
|
|
||||
| ### User | ||||
|
|
||||
| When npm is run as root, scripts are always run with the effective uid | ||||
|
|
@@ -350,11 +364,6 @@ file. | |||
| preinstall or install script. If you are doing this, please consider if | ||||
| there is another option. The only valid use of `install` or `preinstall` | ||||
| scripts is for compilation which must be done on the target architecture. | ||||
| * Scripts are run from the root of the package folder, regardless of what the | ||||
| current working directory is when `npm` is invoked. 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`. | ||||
|
|
||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Extra space |
||||
| ### See Also | ||||
|
|
||||
|
|
||||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.