How to set the NODE_ENV with npm script on windows? - node.js

I am trying to understand how to be able to set the NODE_ENV variable during my start script in node. I am using a windows machine and I have seen some answers about this for mac/linux but I cannot figure it out on windows (disclaimer: I am new to windows shell scripting).
Currently I have this:
"scripts": {
"serve_dev": "set NODE_ENV=development && nodemon index.js",
},
Which does not work. I have also tried adding $env:NODE_ENV... and that did not work as well. Does someone know how I can do this?
Thanks in advance for any help!

I have resolved this issue and it was due to the spaces. The correct implimentation is:
"scripts": {
"serve_dev": "set NODE_ENV=development&&nodemon index.js",
},

Related

Set environment variable through Typescript script

I want to create a hash on build and set is as environment variable. It should be accessible by node.
Firstly I wrote a bash script, exported the environment variable in the script and sourced it in the package.json.
Problem is node doesn't know the source command.
Now I rewrote the script in Typescript (due to the whole project using TS not JS).
In the script I set the variable as follows:
process.env.VARIABLE = hashFunction(path);
The function is called through a script in package.json
"hash": "ts-node path/to/script.ts"
The function works as it should, but the environment variable is not set. Can someone help me to resolve this? Is it possible to return the string outside of the script and set it from there?
If possible i'd like to not use an external package.
Thank you :)
Update:
I used a bash script, but with a typescript script it'd work the same way. For bash the console.log is replaced with echo.
script.ts
console.log("2301293232") // The hash created by the script
package.json
"scripts": {
"build": "yarn run hash react-scripts build", // omit &&
"hash": "ENV_VAR=$(ts-node script.ts)"
}
So I did the following:
The script returns the checksum to the console/standard output. But I'll capture it before and set the printed value as environment variable in the package.json file. This will work as long as its the same process which starts the build.
That is why neither
"scripts": {
"build": "yarn run hash && react-scripts build"
}
nor
"scripts": {
"build": "react-scripts build",
"prebuild": "ENV_VAR=$(ts-node script.ts)"
}
will work. In both examples a new process will be started and the environment variable will be lost.
Can't (easily) change environment variables for parent process
You can change/set the environment for the currently running process. That means that when ts-node runs your program, you are changing the environment variables for your script and for ts-node.
After your script is finished running, ts-node stops, and the environment changes are lost. They don't get passed back to the shell.
Changing another process's environment
Changing the environment variables for the parent process (the shell) is a much more complicated process and depends on your OS and upon having the correct permissions. For linux, one such technique is listed here. In Windows, you can find some hints by looking at this question.
Other options
Your other option might be to just return a string that your shell understands, and run that.

Multiple terminal commands in Windows

this is my first question ever on StackOverflow.
I am a Mac user and I can normally run multiple command-line commands simply by using & e.g.
gulp & 11ty/eleventy --serve
I have been informed that this doesn't work on Windows. But this does work on Windows:
gulp "&" 11ty/eleventy --serve
Therefore, in my package.json I’ve had to do this (below), and it doesn't look so smart, to me:
"scripts": {
"dev": "gulp & npx #11ty/eleventy --serve",
"dev-win": "gulp \"&\" npx #11ty/eleventy --serve"
}
Is there a better way? one way that works for both Mac and Windows terminals?

Use absolute imports in nextJs App on Windows

I follow this post to configure NextJs to use absolute paths, like this:
//package.json:
...
"scripts": {
"dev": "NODE_PATH=. next",
"build": "NODE_PATH=. next build",
"start": "next start"
},
NodeJs, npm and nextJs app are up to date both on windows and Linux.
It works on Linux, but when i try to build on a Windows, it fails with the error
'NODE_PATH' is not recognized as an internal or external command, operable program or batch file.
Update
I find out that npm is just running SO scripts, so in windows, the equivalent for:
NODE_PATH=. next
wold be:
set NODE_PATH=. & next
But doesn't work because the command set does not affect the context of the second command
set X=1 & echo %X% returns
%X% //for first run. doesn't consider it a variable because doesn't know her
1 //for second run
Can't figure out how to overcame this last problem
works from outside:
>set NODE_PATH=.
>npm run dev
Should be some better solution somewhere

Node Environmental variable on Windows

I noticed this strange behavior which is not a big deal, but bugging the heck out of me.
In my package.json file, under the "scripts" section, I have a "start" entry. It looks like this:
"scripts": {
"start": "APPLICATION_ENV=development nodemon app.js"
}
typing npm start on a Mac terminal works fine, and nodemon runs the app with the correct APPLICATION_ENV variable as expected. When I try the same on a Windows environment, I get the following error:
"'APPLICATION_ENV' is not recognized as an internal or external command, operable program or batch file."
I have tried the git-bash shell and the normal Win CMD prompt, same deal.
I find this odd, because typing the command directly into the terminal (not going through the package.json script via npm start) works fine.
Has anyone else seen this and found a solution? Thanks!!
For cross-platform usage of environment variables in your scripts install and utilize cross-env.
"scripts": {
"start": "cross-env APPLICATION_ENV=development nodemon app.js"
}
The issue is explained well at the link provided to cross-env. It reads:
Most Windows command prompts will choke when you set environment variables with NODE_ENV=production like that. (The exception is Bash on Windows, which uses native Bash.) Similarly, there's a difference in how windows and POSIX commands utilize environment variables. With POSIX, you use: $ENV_VAR and on windows you use %ENV_VAR%.
I ended up using the dotenv package based on the 2nd answer here:
Node.js: Setting Environment Variables
I like this because it allows me to setup environmental variables without having to inject extra text into my npm script lines. Instead, they are using a .env file (which should be placed on each environment and ommitted from version control).
You should use "set" command to set environment variables in Windows.
"scripts": {
"start": "set APPLICATION_ENV=development && nodemon app.js"
}
Something like this.

Why does nodejs debugger ignore breakpoints when it's run in npm configuration?

I have a node.js code in WebStorm. I debug the code by using node.js configuration and it works fine.
However, once I run debug mode in npm configuration, WebStorm ignores the breakpoints. I tried to add the variables "--debug" and "--debug-brk" to 'Arguments' field and it still didn't solve the problem.
Please check out this tutorial: http://pavelpolyakov.com/2016/05/01/webstorm-npm-tasks-debug/
You need to add $NODE_DEBUG_OPTION in your npm script.
Here's the original issue on the WebStorm tracker.
You need to add $NODE_DEBUG_OPTION (or %NODE_DEBUG_OPTION% on Windows) in your npm script. Please note that NODE_DEBUG_OPTION should be placed right after node, where node parameters are expected, for example:
"scripts": {
"start": "node $NODE_DEBUG_OPTION ./bin/www"
}

Resources