To find the directory an npm script uses on your Mac, check which package owns the script, then print the directory from inside that script. npm runs a script from its package folder, even when you start npm run elsewhere. In a workspace project, the selected workspace package may be the relevant folder. The steps below help you identify the exact path and distinguish it from the folder where you typed the command.
1. Find the package and script you are running
Open Terminal and go to the project folder where you normally run npm. macOS uses zsh by default in Terminal, though your shell can be changed. Use pwd to display your current directory, then list the files to see whether that folder contains a package.json file. Apple’s Terminal guide explains entering commands in Terminal and using tools such as ls.
pwd
ls
If you are unsure which package defines the script, inspect package.json in the current folder. You can display it with cat package.json, or open it in a text editor. Find the script name under the scripts object. For example, in "scripts": { "build": "node build.js" }, the script name to run is build. Don’t assume the top-level project folder owns the script if this is a workspace repository.
2. Check npm’s expected directory
For current npm versions, scripts run from the root of the package folder, regardless of the directory from which you invoked npm run. In a standard project, that is the folder containing the package’s package.json. In a workspace project, npm can run the script in the context of a selected workspace; the workspace’s package is the one to check. See npm’s documentation on script working directories and running scripts in workspaces.
This documented behavior is a good starting answer, but if the script changes directories internally or you need to confirm what a particular command sees, print the path during execution. A shell command such as cd inside the script can change the directory for commands that follow it, so the package folder alone may not tell the whole story.
3. Print the directory from inside the script
The clearest check is to temporarily add pwd at the beginning of the script command in package.json. For example, change:
"build": "node build.js"
to:
"build": "pwd && node build.js"
Then run the script as usual:
npm run build
The path printed before the build command is the shell’s working directory at that point. This diagnostic uses &&, so node build.js runs only if pwd succeeds. After checking, remove the diagnostic prefix and restore the original command. Review the file before saving if the original script already uses shell operators, quotes, or a wrapper command; preserve its intended order and syntax.
If the script is a Node.js file and you can edit it, another direct check is to temporarily add console.log(process.cwd()) near its start. Node documents process.cwd() as the current working directory of the Node.js process. That reports the directory Node sees when the line executes; if the program later changes its working directory, the value can change.
4. Read the result and account for workspaces
Compare the printed path with the path shown by pwd before you ran npm. They may differ: npm’s script directory is the package root, while your Terminal directory is where you launched the command. For example, if your Terminal starts in a repository root but you run a script defined by a package under packages/site, the script’s package context can be that workspace. npm documents selecting one with --workspace; you can also run a script from inside the workspace directory.
npm run build --workspace=site
Use the workspace name or path that matches the project’s workspaces configuration. If the output is unexpected, confirm that you selected the right script and workspace, then check whether the script itself contains cd or invokes another tool that changes directories. A child process may also have its own working-directory setting; inspect that tool’s configuration if its behavior differs from the parent script.
What INIT_CWD tells you
npm provides INIT_CWD for the directory you were in when you invoked npm run. That value can help when a script needs to know where the caller started, but it is not the script’s current working directory. Use pwd inside the shell script or process.cwd() inside Node.js to inspect the directory the running process sees. The distinction is especially useful when a relative file path works from one invocation location but not another.
For a quick diagnosis, start with npm’s package-root rule, select the intended workspace if needed, and print pwd inside the script to confirm the actual path at the point where it runs.
Sources
Scripts — npm Docs
Workspaces — npm Docs
Process — Node.js
Execute commands and run tools in Terminal on Mac — Apple Support
Change the default shell in Terminal on Mac — Apple Support