Switching between Node projects on Windows usually works fine until you hit a package with cross-platform build scripts or an active Corepack configuration. You type npm run build or pnpm install, and your terminal blows up with missing command errors, PATH collisions, or silent execution drops.

The failures happen at the intersection of three things: Windows-incompatible shell primitives in package.json, Corepack shims clashing with version managers like fnm or nvm-windows, and PowerShell refusing to parse chained POSIX syntax.

The Broken State

You clone a repository, switch to the right Node version using Fast Node Manager (fnm), enable the repo’s declared package manager via Corepack, and run the setup script.

text
> [email protected] build
> NODE_ENV=production rimraf dist && tsc

'NODE_ENV' is not recognized as an internal or external command,
operable program or batch file.

Or, if your IDE defaulted to PowerShell:

text
The token '&&' is not a valid statement separator in this version.

Or you hit the Corepack shim paradox after switching Node versions:

text
Internal Error: Dynamic require of "C:\Users\username\AppData\Local\fnm_multishells\4132_1711200000000\node_modules\corepack\dist\pnpm.js" is not supported

These errors show up because Node on Windows executes scripts through cmd.exe by default, not PowerShell and not Bash. When a script assigns inline environment variables (NODE_ENV=production), cmd.exe looks for a binary called NODE_ENV. When Corepack activates shims, it places wrapper binaries in a shared directory that may point to an older Node install path from your version manager that no longer exists in your current terminal session.

Version Managers and Corepack Shims on Windows

fnm is generally faster and cleaner than nvm-windows because it uses native Windows directory junctions instead of mutating system-wide registry variables on every switch. But Corepack creates complications for both.

When you run corepack enable, Corepack places wrapper symlinks (pnpm.cmd, yarn.cmd) into your active Node installation bin folder or a global directory. When fnm switches versions, your PATH changes to point to a new isolated multishell directory.

If you installed Node through fnm or nvm-windows, do not install Yarn or pnpm with npm install -g yarn pnpm. That pollutes the version-specific global prefix and breaks whenever you swap Node versions.

The Working FNM Setup

Install fnm using Winget or Cargo, and configure your profile to update your current shell environment automatically.

powershell
winget install Schniz.fnm

Open your PowerShell profile with notepad $PROFILE and add this exact block:

powershell
# fnm environment configuration
$env:FNM_LOGLEVEL = "error"
fnm env --use-on-cd --shell powershell | Out-String | Invoke-Expression

For Git Bash users, put this in ~/.bashrc:

bash
eval "$(fnm env --use-on-cd --shell bash)"

Create a .node-version or .nvmrc file in the root of your project:

text
20.12.2

Navigate into the folder. fnm changes your Node environment instantly on directory entry.

Stabilizing Corepack

Corepack ships with Node, but on Windows it struggles when switching between multiple Node runtimes because its shims can point to stale paths.

To fix this, define a dedicated COREPACK_HOME environment variable so downloaded package manager binaries persist in one persistent directory instead of being buried in dynamic multishell paths.

Set this permanently in PowerShell:

powershell
[System.Environment]::SetEnvironmentVariable('COREPACK_HOME', "$HOME\.cache\corepack", 'User')
$env:COREPACK_HOME = "$HOME\.cache\corepack"

Now enable Corepack strictly for the current user and install your project-pinned package manager:

powershell
corepack enable --install-directory $HOME\.local\bin

Ensure $HOME\.local\bin is at the beginning of your user PATH. If you prefer to avoid manual directories, let Corepack write directly to the Node folder, but remember to run corepack enable once per installed major Node version:

powershell
fnm use 20.12.2
corepack enable
corepack prepare pnpm@9.0.6 --activate

Verify that the active executable points where you expect:

powershell
Get-Command pnpm | Select-Object -ExpandProperty Source
pnpm --version

It should return 9.0.6, not your global system version.

The Script Shell Trap: CMD vs PowerShell vs Bash

By default, npm runs scripts on Windows using cmd.exe /d /s /c. This causes script failures whenever your team members on macOS or Linux write scripts with:

  • POSIX variable assignment (VAR=value command)
  • Semicolons for chaining (lint; test)
  • Single quotes (eslint 'src/**/*.ts')
  • POSIX file operations (rm -rf, cp, mv)

Here is how to solve each without forcing your whole team to rewrite their pipelines.

Option 1: Configure npm to Use Bash

If your developers have Git for Windows installed, you already have a full Bash distribution at C:\Program Files\Git\bin\bash.exe. You can tell npm to execute all script commands directly in Bash.

Open your local project .npmrc or the global %USERPROFILE%\.npmrc and add:

ini
script-shell = "C:\\Program Files\\Git\\bin\\bash.exe"

Test it immediately. Add this to your package.json:

json
{
  "name": "windows-shell-fix",
  "version": "1.0.0",
  "scripts": {
    "test-shell": "VAR=test && echo \"Shell variable is $VAR\""
  }
}

Run:

powershell
npm run test-shell

Output:

text
> [email protected] test-shell
> VAR=test && echo "Shell variable is $VAR"

Shell variable is test

This single line inside .npmrc fixes 90 percent of cross-platform script incompatibilities. Single quotes, chained operations, and environment variables will work just like they do on Linux.

Option 2: Pure Node Tools for Cross-Platform Teams

If you work in a locked-down corporate environment where Git Bash is not installed in standard locations, relying on a hardcoded script-shell path will break your teammates’ machines. In that scenario, use zero-dependency JavaScript replacements instead of shell primitives.

Replace POSIX assignments with cross-env, and replace POSIX deletions with native Node flags.

Bad script:

json
{
  "scripts": {
    "clean": "rm -rf dist",
    "build": "NODE_ENV=production tsc"
  }
}

Good script:

json
{
  "scripts": {
    "clean": "node -e \"fs.rmSync('dist', { recursive: true, force: true })\"",
    "build": "cross-env NODE_ENV=production tsc"
  }
}

Node 14.14+ and Node 16+ support fs.rmSync(path, { recursive: true, force: true }) natively. You do not need rimraf as an extra dependency, and you do not need bash.exe.

Edge Cases and Gotchas

Quoting Rules with Node -e Commands

When using node -e directly in package.json scripts on Windows, double quotes are required for the external argument, and single quotes must be used internally.

json
{
  "scripts": {
    "bad": "node -e 'console.log(process.platform)'",
    "good": "node -e \"console.log(process.platform)\""
  }
}

cmd.exe treats single quotes as literal text characters, not quote delimiters. If you use the bad line above without Git Bash configured, Windows will pass 'console.log(process.platform)' verbatim to the V8 engine, which crashes with a SyntaxError: Unexpected token '\''.

If you use nvm-windows instead of fnm, running nvm use <version> changes a symbolic link located at C:\Program Files\nodejs.

If your terminal does not have proper permissions, or if Windows Developer Mode is turned off, this command fails silently or prints:

text
exit status 1: Access is denied.

Fix this without running your terminal as Administrator every time:

  1. Open Windows Settings.
  2. Go to System -> For developers.
  3. Toggle Developer Mode to On.

Developer Mode grants standard user accounts the SeCreateSymbolicLinkPrivilege permission, allowing both nvm-windows and fnm to link directories without UAC prompts.

When using pnpm on Windows alongside fnm, running pnpm install inside a directory that is itself a symlink or junction can make Node resolve modules incorrectly due to realpath evaluation.

Add this setting to your root .npmrc file:

ini
node-linker=hoisted
symlink=true

Or run Node scripts with the --preserve-symlinks flag if your build process references assets outside the immediate workspace root.

Verification

To verify that your entire chain is healthy, run this diagnostic sequence in a standard, normal PowerShell window:

powershell
fnm current
corepack --version
npm config get script-shell
node -e "console.log(JSON.stringify({ platform: process.platform, arch: process.arch }))"

The output should show your project-pinned Node version, the Corepack version, your configured shell path (or undefined if using pure Node fallbacks), and valid platform JSON:

text
v20.12.2
0.28.0
C:\Program Files\Git\bin\bash.exe
{"platform":"win32","arch":"x64"}

No special permission prompts, no dynamic path resolution errors, and no shell quoting syntax errors during daily builds.

More in JavaScript