Passing command line variables to npm scripts in Windows environment

I have a project setup where I need to clone two git repositories. The main repo contains a template and there’s a nested repo inside it that needs separate handling. I want to automate this with an npm script that takes a folder name as input.

I’m trying to create a script that clones the main repository first, then navigates into that directory and clones the second repository. The tricky part is passing a dynamic folder name.

Here’s what I tried:

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

I run it with: FOLDER_NAME=my_new_project npm run setup_project

But instead of using the actual variable value, it literally creates a folder called $FOLDER_NAME.

I also attempted using npm arguments:

"setup_project": "git clone path/to/main_repo $FOLDER_NAME && cd $FOLDER_NAME && git clone path/to/nested_repo"
npm run setup_project --FOLDER_NAME my_project

And with double dashes:

npm run setup_project -- --FOLDER_NAME=my_project

Same issue occurs. The variable doesn’t get resolved properly on Windows. I’m using Git Bash as my terminal and npm version 2.14.20.

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

windows cmd and git bash handle variables completely differently. quick fix: use the npm_config_ prefix. change your script to "setup_project": "git clone path/to/main_repo %npm_config_name% && cd %npm_config_name% && git clone path/to/nested_repo" then run npm run setup_project --name=my_project. the % syntax works way better on windows than $.

Been there before with npm scripts on Windows. Variable handling sucks because Windows uses different syntax than Unix systems.

Try this approach:

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

Then run: npm run setup_project --folder=my_new_project

The %npm_config_folder% syntax works on Windows. You can also use cross-env for cross-platform compatibility:

npm install cross-env --save-dev

"setup_project": "cross-env git clone path/to/main_repo $FOLDER_NAME && cd $FOLDER_NAME && git clone path/to/nested_repo

Honestly though, I’d ditch npm scripts for this. Multi-step setup processes are better suited for automation tools.

I use Latenode for similar repository workflows. Create a workflow that takes your folder name, runs git commands in sequence, handles errors properly, and sends notifications when done.

No more fighting Windows variable syntax or npm limitations. You get proper error handling and can easily add more setup steps later.

Check it out: https://latenode.com

Windows npm scripts and environment variables are a pain. Your issue is that Windows doesn’t expand Unix-style variables in npm scripts like you’d expect. I’ve found the cleanest fix is making a simple Node.js wrapper script. Create setup.js in your project root:

const { execSync } = require('child_process');
const folderName = process.argv[2] || 'default_project';

const commands = [
  `git clone path/to/main_repo ${folderName}`,
  `cd ${folderName} && git clone path/to/nested_repo`
];

commands.forEach(cmd => {
  console.log(`Running: ${cmd}`);
  execSync(cmd, { stdio: 'inherit', shell: true });
});

Then update your package.json:

"setup_project": "node setup.js"

Run with: npm run setup_project my_new_project

This completely bypasses Windows shell variable issues and gives you better error handling. I’ve used this on multiple Windows dev machines without any problems. Way more reliable than fighting npm’s variable substitution weirdness.