Passing environment variables to npm scripts in Windows environment

I have a setup where I need to automate repository cloning for new projects. My workflow involves cloning a main repository and then cloning a nested repository inside it. I want to create an npm script that can accept a project name as a variable and use it in the script commands.

Here’s what I’m trying to accomplish:

"setup_project": "git clone path/to/main_repo %FOLDER_NAME% && cd %FOLDER_NAME% && git clone path/to/nested_repo"

I’ve attempted running it with:
FOLDER_NAME=my_new_project npm run setup_project

But Windows doesn’t seem to recognize the variable syntax. The script literally creates a folder called %FOLDER_NAME% instead of using the actual variable value.

I also tried using npm’s argument passing:

npm run setup_project --FOLDER_NAME my_project

And with double dashes:

npm run setup_project -- --FOLDER_NAME=my_project

None of these approaches work on Windows. The variable substitution that works fine on Unix systems doesn’t seem to work here. I’m using Git Bash on Windows with npm version 2.14.20.

How can I properly pass and use variables in npm scripts on Windows? The solution needs to be a single command line.

try using cross-env: cross-env FOLDER_NAME=my_project npm run setup_project. it let’s you use vars in your npm script and works on all platforms, solving that windows issue.

Been down this rabbit hole way too many times with client deployments. The issue isn’t just Windows vs Unix - you’re trying to solve automation with manual workarounds.

Stop wrestling with npm scripts and shell differences. Automate the entire project setup workflow properly. I handle this by creating simple automation that watches for new project requests and handles repository cloning automatically.

Set up a workflow that takes the project name as input, clones your main repo with the right folder name, navigates into it, and clones the nested repo. No more fighting with environment variables or platform-specific scripts.

You get consistent behavior across all environments. No more ‘works on my machine’ issues when someone on your team uses different shells or operating systems.

This scales better too. Need to add more setup steps like installing dependencies, creating config files, or setting up databases? Just extend the workflow instead of making your npm scripts even more complex.

Check out Latenode for building this kind of project automation: https://latenode.com

Fought with this for months across different Windows machines before I found something that actually works. The problem is npm scripts use cmd.exe on Windows, and it handles environment variables differently than other shells. Instead of battling platform quirks, I just went with a batch file. Make a setup.bat with git clone path/to/main_repo %1 && cd %1 && git clone path/to/nested_repo and call it from your npm script like "setup.bat %npm_config_project%". Run with npm run setup_project --project=my_new_project. This skips all the shell weirdness and works the same every time. The batch file does the variable stuff right while npm just passes everything through.

Had this exact problem last year with automated workflows across multiple client projects. Windows cmd handles environment variables completely differently than Unix shells, and Git Bash doesn’t bridge that gap well for npm scripts. Here’s what actually worked: ditch %FOLDER_NAME% and use $npm_config_folder_name in your script instead. Then pass it like npm run setup_project --folder_name=my_project. The npm config variables work across all platforms. Or just write a simple Node.js wrapper that reads process.argv and runs the git commands through child_process.exec(). Way more reliable than wrestling with shell syntax differences, especially for complex automation.