Misc notes on npm script issues and solutions in Windows environment
3 Feb. 2026
Chat with Google Search AI (GSAI)
[Note: GSAI confirmed that by default, npm scripts on Windows use the Command Prompt (cmd.exe), not PowerShell.]
Me: Windows command prompt set env variable and run another command which needs it
GSAI: ...
3. Temporary Scoping (Child Shell)
To ensure the variable is "forgotten" immediately after the command finishes, run it inside a child shell.
- Command:
cmd /C "set "TEMP_VAR=abc" && run_process.exe" - Effect: Once the process finishes and the child shell closes,
TEMP_VARwill no longer exist in your main command prompt session.
...
Me: Set is great! another ai tool suggested setx. Pls comment
GSAI:
setx is powerful but comes with a major "gotcha" for your specific use case: it doesn't affect the current session.Here is the breakdown of why you might use it and when you definitely shouldn't:
1. The Big "Gotcha"
If you run
setx MY_VAR value and then immediately try to run a command that uses MY_VAR, that command will not see the variable.setxwrites to the Windows Registry for future sessions.- To use the variable in the same window where you set it, you must use
set.
2. When to use
setxUse
setx only if you want the variable to stay "on" even after you restart your computer or open a brand new terminal.- User Variable:
setx VAR_NAME "value"(Default). - System-wide Variable:
setx /m VAR_NAME "value"(Requires Administrator privileges).
...
Me: I need it for an npm build script.
"scripts":
{
"build:ghp": "Set DEPLOY_TARGET=ghp && next build",
...
}
I think the above will do the needful
GSAI: That script will work on Windows CMD, but there is a major trap you need to avoid: whitespace.
1. The Whitespace Trap
In your current script:
"Set DEPLOY_TARGET=ghp && next build"On Windows, the space before the
&& will be included in the variable's value.- Result:
DEPLOY_TARGETwill be"ghp "(with a space). - Fix: Remove the space before
&&
...
Me: [GSAI suggested cross-env] cross-env is new to me. I don't want to spend time learning that now. As of now, this repo is private to me and I use only Windows.Let me do some tests here. Can you provide me a simple script that will just echo value of DEPLOY_TARGET
GSAI: To test this on Windows without external tools, use the
call trick.Since CMD evaluates variables before running the line, a simple
set VAR=VAL && echo %VAR% will show nothing (or the old value). Using call echo %VAR% forces a second pass of the parser, allowing it to see the newly set value.Test Script for
package.jsonAdd this to your
scripts object:Why this works:
set "VAR=VAL": Using quotes around the assignment is the recommended practice to ensure no accidental trailing spaces are captured in the variable.- No space before
&&: This is critical. In CMD, a space before&&becomes part of the variable value (e.g.,"ghp "). call echo %DEPLOY_TARGET%: Thecallcommand triggers a second parsing phase, which picks up the value set just milliseconds prior in the same line.
To run the test:
In your terminal, simply type:
npm run test:envIt should output
ghp exactly. If you see %DEPLOY_TARGET% or nothing at all, ensure there are no spaces between the last quote and the && symbols....
[GSAI suggested using [ ] characters to confirm that space has not crept into the env variable.]
Me: script:
"test:env": "set \"DEPLOY_TARGET=ghp\"&& call echo [%DEPLOY_TARGET%]",
"test:env2": "set \"DEPLOY_TARGET=ghp\" && call echo [%DEPLOY_TARGET%]",
--
Command output
stripdowncomm> npm run test:env
> test:env
> set "DEPLOY_TARGET=ghp"&& call echo [%DEPLOY_TARGET%]
[ghp]
stripdowncomm> npm run test:env2
> test:env2
> set "DEPLOY_TARGET=ghp" && call echo [%DEPLOY_TARGET%]
[ghp]
stripdowncomm>
==========================
What the above confirms is that both work. I prefer the test:env2 version as that is more readable.
...
Me: U wrote: Since you are using the "key=value" quoting style, you are safe to use the more readable test:env2 version. The quotes are doing the heavy lifting to keep your variable clean of that extra space.
-----------
Yes, that's what I want. it is ugly to have && without a preceding space. Windows provides the double quote as a mechanism to delimit the string assigned to the env variable. We are using it and so we can avoid the ugly && without preceding space issue.
...
Me: Good suggestion about next.config. Here's my code (.ts file). Please validate
const isDev = process.env.NODE_ENV !== "production";
const deployTarget = process.env.DEPLOY_TARGET;
let basePath = "";
let assetPrefix = "";
if (!isDev) {
if (deployTarget === "ghp") {
basePath = "/stripdowncommghp";
assetPrefix = basePath;
} else {
console.warn(`Unknown DEPLOY_TARGET (enclosed in single quotes): '${deployTarget}'.
Assuming local or custom domain build (no basePath).`);
}
}
GSAI: Your logic is solid and follows standard Next.js configuration patterns for GitHub Pages. Since you are using a
.ts file, your validation check is actually the best way to catch any future environment issues.1. Code Validation
The comparison
deployTarget === "ghp" will work perfectly with your current package.json setup because:- The
set "VAR=VAL"syntax ensures no trailing spaces. - The quotes are stripped by CMD, so
process.env.DEPLOY_TARGETreceives the literal stringghp.
2. One Minor Improvement
If you want to be extremely robust against accidental whitespace (just in case someone else edits the
package.json later without the "quote trick"), you can add a .trim() to your assignment:3. The
console.warn CheckYour warning is clever:
console.warn(Unknown DEPLOY_TARGET (enclosed in single quotes): '${deployTarget}'.)If your script ever fails, this log will show you exactly what happened:
- If it shows 'ghp ', you'll know a space snuck in.
- If it shows ''ghp'', you'll know double-quotes were captured.
======== end GSAI chat extracts ===========
Later I added one more small confirmation test which too worked.
In package.json:
"test:env3": "set \"DEPLOY_TARGET=ghp\" && cmdecho.bat",
-----
cmdecho.bat contents:
echo DEPLOY_TARGET: [%DEPLOY_TARGET%]
-----
Run output:
stripdowncomm> npm run test:env3
> test:env3
> set "DEPLOY_TARGET=ghp" && cmdecho.bat
--snipped--\stripdowncomm>echo DEPLOY_TARGET: [ghp]
DEPLOY_TARGET: [ghp]
stripdowncomm>
-----------------
I think that indicates that this solution should work with build too.
Comments
Post a Comment