-
Notifications
You must be signed in to change notification settings - Fork 248
Variable assignement is not working when the assignment requires a command execution #192
Comments
Did you find a solution to this? I want to assign a commit hash to an environment variable in a build script also but cannot get this to work. |
I tried doing |
@bemipefe @buddythumbs After wasting a full day going down this rabbit hole I figured it out. The issue is that npm uses #in .npmrc
script-shell = "C:\\windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe"
#using the CLI
npm config set script-shell = "C:\\windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe" Then you can use command substitution. //in package.json
{
"scripts": {
"start": "cross-env HASH=$(git show --no-patch --format=%h) node echo.js"
}
}
// echo.js
console.log(process.env.HASH) C:\Users\me\my-project>npm start
> [email protected] start C:\Users\me\my-project
> cross-env cross-env HASH=$(git show --no-patch --format=%h) node echo.js
0fbd4ad 🎉 😄 Now I have to figure out how to use a different .npmrc per platform so every developer doesn't have to create their own config file. 😕 |
@parkerault Is this a bug in npm then? EDIT: Looks like changing the shell interferes with installations. For example |
@Rovyko Yeah, this feels like fighting the platform, which makes me think that once you need to do more than set static variables it's time to break out the javascript. |
I agree. I recommend folks use cross-env for setting environment variables and calling into scripts. Keep it simple. |
Using ; did it
|
@parkerault thanks, this is great! For anyone having trouble with Powershell I'm using this JS solution: https://stackoverflow.com/a/55284054/2771889 |
because npm does not support command substitution on windows ref https://github.com/kentcdodds/cross-env/issues/192\#issuecomment-513341729
cross-env
version: 5.2.0node
version: 6.14.4npm
(oryarn
) version: 3.10.10Relevant code or config
What you did:
npm install
What happened:
The command printed the following output:
The variable content is node -e console.log(process.arch)
The expected output was:
The variable content is x64
or
The variable content is ia32
Problem description:
Instead of the command output the variable is filled with the command itself ("node -e console.log(process.arch)"). Removing the double-quotes gives syntax error.
The command runs just fine if a space character is put after "modarch=" but of course the result is never assigned to modarch.
The text was updated successfully, but these errors were encountered: