Node.js DEBUG=appname nodemon not working on windows - node.js

Am following the tutorial by Mosh in Node.js and I am not able to make this line of code work.
DEBUG=app:db nodemon index.js
I got an error which is:
'DEBUG' is not recognized as an internal or external command,
operable program or batch file.`
While in his end it is working fine.
Is this only works on MAC? I've also tried
set DEBUG=app:db nodemon index.js
But still get the same error.
Well, I've seen and tried these answers but still didn't work for me.
Could someone explain why this doesn't work?

Try this "set DEBUG=app:* & nodemon index.js" on Windows. Then refresh your browser you will see the connection to the database.

Using windows, you must separate commands with && separator:
"scriptCommand": "set DEBUG=app:db&& nodemon index.js"
Please note the there is NO space between db and &&. This is intentional, as the variable space spreads all the way to the && wall - meaning it will add an unintentional space after db to your variable.
Also, you perhaps would like to try the very useful and self-explanatory cross-env library which allows you to use 1 syntax to declare an environment variable in any env (Win, Mac, Linux) the project is initialized in

Related

Beginner Working Through Authentication (Node.js example)

Currently working through some of the documentation examples from Jack Henry and I've ran into an issue on the Authentication (Node.js) example project (https://jackhenry.dev/open-api-docs/consumer-api/quickstarts/authentication/)
For reference, here is the git repository being used https://github.com/Banno/consumer-api-openid-connect-example
I have Node.js/npm installed locally and after using "npm install", when I try to use "npm start", I get an error. Screenshot of Error
> #banno/consumer-api-openid-connect-example#1.0.0 start
> ENVIRONMENT='local' node server.js
>'ENVIRONMENT' is not recognized as an internal or external command,
>operable program or batch file.
I've not modified any of the package.json file where this line is located, which is throwing the error.
Any suggestions as to what could be the issue? Thanks!
The Node.js examples more or less assume a *nix-compatible operating environment (e.g. Linux, Unix, macOS) and apparently do not work on vanilla Windows.
The suggestions above from other folks for working around how to set up the command line variable or using cross-env might be good to try.
It might also be worth considering using Windows Subsystem for Linux to get that *nix-compatible operating environment.
It looks like Microsoft has some documentation on how to Install Node.js on Windows Subsystem for Linux (WSL2).

Mysterious parameter -r in Nodemon

I've been searching on how to reload env on the development environment while using Nodemon.
The command that spread all over on the internet is nodemon -r dotenv/config bin/www.js, this is actually working, but look at the command, what is the -r thing? I'm out of curiosity.
I've been searching on the official documentation with no luck. https://github.com/remy/nodemon/wiki
Anyone care to explain, thank you.
I finally found an answer.
Quoted from Nodemon docs:
nodemon wraps your application, so you can pass all the arguments you would normally pass to your app
https://github.com/remy/nodemon#usage
So basically any arguments that unknown to Nodemon will redirect to Node.js app, so -r is passed to the Node.js, which means "Require module".
See: https://nodejs.org/dist/latest-v12.x/docs/api/cli.html#cli_r_require_module
-r is not a valid argument option
Take a look at
https://github.com/remy/nodemon/blob/master/lib/cli/parse.js
for all supported options

WARNING: NODE_ENV value of 'test ' did not match any deployment config file names

Receiving this warning on running a node.js app despite all testing suggesting everything fine.
I've included the below code in my app to fault find:
console.log('NODE_ENV: ' + config.util.getEnv('NODE_ENV'));
console.log('NODE_CONFIG_DIR: ' + config.util.getEnv('NODE_CONFIG_DIR'));
And the output in terminal is:
NODE_ENV: test
NODE_CONFIG_DIR: C:\Users\[username]\OneDrive - [Company Name]\Documents\projects\[project name]\server\config
Terminal Output
Inside that folder (verified by copying and pasting the URI into explorer) are two files:
default.json
test.json
Config folder contents
That seems to be correct to me, I've checked the guidance and can't see anything out, checked google hits and the answers don't appear to relate. Any help would be greatly appreciated.
Though the mentioned package in the accepted answer resolves the issue, it is good not to get more dependencies in your project's package.json when the same can be sorted with a simple tweak as below: -
In your package.json file by omitting the space before &&. This will detect the environment(s) correctly without extra space after the name.
Quick answer:
Use something like cross-env on windows environments: npm cross-env
From what I can find, this appears to fall into the 'Windows issues with NODE_ENV' category. Setting the NODE_ENV separately, prior to starting the app results in the environment working correctly; any manipulation of NODE_ENV inside a package.json script on its own results in failure.
This answer on another question led me to the package cross-env, and when implemented as identified in the answer resolves the issue.
If anyone else has the same error be sure to run the jest command from your project's root directory if you directly use jest cli instead of an npm script

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.

nodejs installation on openshift - how to add --harmony flag to startup

I'm trying to deploy an app of mine using openshift PaaS. Everything working fine so far, the only thing I couldn't accomplish was to set the --harmony flag when node starts up.
They're using node-supervisor to startup the process but won't add the options I added to the node.env file :(
anyone ever ran into the same problem?
You would need to use a DIY gear and have the action hook call the custom command to launch it. Right now there is no way to modify the supervisor script to add additional flags.
It works for me if I add the harmony flag to main property in my package.json.
i.e.
"main": "--harmony server.js"
instead of
"main": "server.js"
I'm also using https://github.com/ramr/nodejs-custom-version-openshift, so not sure if this works without that.

Resources