Skip to content

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

Open
wants to merge 6 commits into
base: latest
Choose a base branch
from

Conversation

tarekwfa0110
Copy link

This pull request updates the documentation for npm scripts, specifically the scripts.md file.

What:

  • Clarified that scripts are always run from the root of the package folder in npm v7 and later.
  • Added a "Historical Behavior in Older npm Versions" section to explain that while generally true for npm v6 and earlier, there were inconsistencies and that process.cwd() could be used as a safeguard.
  • Ensured the INIT_CWD environment variable is mentioned as a way for scripts to access the original working directory.
  • Provided links to the npm v7 release notes and the relevant GitHub issue discussing script working directory reliability in older versions.

Why:
This change aims to provide clearer and more accurate documentation regarding the working directory of npm scripts, addressing potential confusion, especially for users working with or migrating from older npm versions. This helps resolve the points raised in issue #8305.

References

Fixes #8305

@tarekwfa0110 tarekwfa0110 requested a review from a team as a code owner May 19, 2025 13:37
Copy link
Contributor

@owlstronaut owlstronaut left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the change in general, just a little cleanup and we'll run the tests on it

Comment on lines 239 to 240
In npm v7 and later, scripts are always executed from the root of the package folder, regardless of your current working directory. This behavior is now reliable and consistent.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
In npm v7 and later, scripts are always executed from the root of the package folder, regardless of your current working directory. This behavior is now reliable and consistent.

This can be removed, repeat of the above section.

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

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

Extra space

@tarekwfa0110 tarekwfa0110 changed the title docs: fix overrides example in package-json.md #8283 docs: Clarify script working directory behavior (fixes #8305) May 19, 2025
@tarekwfa0110
Copy link
Author

tarekwfa0110 commented May 20, 2025

I like the change in general, just a little cleanup and we'll run the tests on it

@owlstronaut
I applied the requested changes. If you have other feedbacks, just let me know.

@tarekwfa0110 tarekwfa0110 requested a review from owlstronaut May 20, 2025 06:03
@tarekwfa0110 tarekwfa0110 changed the title docs: Clarify script working directory behavior (fixes #8305) docs: Clarify script working directory behavior (fixes #8305) May 20, 2025
@tarekwfa0110
Copy link
Author

@owlstronaut
Sorry for the mistake I edited the header to not include any spaces at its end. That should resolve the test now I assume.

@tarekwfa0110 tarekwfa0110 requested a review from owlstronaut May 20, 2025 17:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[DOCS] Working directory of npm scripts in older versions of npm
2 participants