The $NODE_CONFIG environment variable is malformed JSON - node.js

I am new in Node. I am trying to start a node service by using the following command --
export NODE_CONFIG_DIR=/var/www/api/config NODE_ENV=development PORT=3018; node /var/www/api/app.js
Inside the config directory I have the following file development.json
{
"general": {
"port": 3018
},
"database": {
"connectionString": "mongodb://***:***#url:port/api?replicaSet=set-578f3ed7000587"
},
"environmentName": "Development"
}
And I am getting the following line after starting node
The $NODE_CONFIG environment variable is malformed JSON
API listening on port 3018!
When I try accessing the api in browser I get 502 bad gateway. The node is running though.
Any help is highly appreciated.

Related

How to add node run option --max-http-header-size and add name to pm2 start command

How would I start a pm2 process with the —max-http-header-size node option, as well as name the process.
I have a server with multiple micro-services, one of the services is a web scraper.
This web scraper accepts requests with headers over the nodejs default 8kb limit. So, to run my app locally have to add the --max-http-header-size node option.
I've cloned my app to the server, but don't know how to set --max-http-header-size, nor do I know how to name the process within the pm2 start command.
So far my attempts have looked like this.
// this sets the name, but I don't know how to add the option `--max-http-header-size`
pm2 start npm --name "{REPONAME}" -- start
pm2 start node --name "scraper" --max-http-header-size 15000 app.js
pm2 start node --max-http-header-size 15000 app.js -- --name "scraper"
The accepted answer by David Harvey did not work for me as of Node 16. Instead, I had to use node_args, something like this:
{
"apps" : [{
"name" : "myapp",
"script" : "./app.js",
"node_args": "--max-http-header-size=256000",
"env": {
"NODE_ENV": "production",
}
}]
}
What you're looking for is called environment variables! You can pass environment variables to pm2 using a file that loads your server like this:
module.exports = {
apps : [
{
name: "myapp",
script: "./app.js",
watch: true,
node_args: "--max-http-header-size=16000"
}
]
}
Here's more about it too:
https://pm2.keymetrics.io/docs/usage/environment/

Unable to configure MongoDB in Angular application that is using Express and Azure Cosmos DB

I am facing strange issue in configuring and using Azure CosmosDB MongoDB Api in angular application, the configuration and code is referred from https://learn.microsoft.com/en-us/azure/cosmos-db/tutorial-develop-mongodb-nodejs-part2 and https://johnpapa.net/angular-cosmosdb-1/, though i was able to configure and run application with express when i am trying to configure and install Mongoose node module getting series of errors :
1) Cannot find module 'mongodb-extjson 2) Cannot find module 'bson-ext' 3) ENOENT: no such file or directory, access 'XXX\node_modules\bson-ext\lib\package.json
I have tried re installing packages but no success.
I am using Angular version 10 with mongoose version 5.11.14
i am getting error at compile time when trying to run it from vs code and trying to launch server/index.js on port 3000.
Your help is greatly appriciated.
You are getting 1 and 2 because the MongoDB driver checks for optional requirements during initialization. You can F5 past them in VS Code. Or install them like so:
node install mongodb-extjson
node install bson-ext
Number 3 is also due to an exception during startup. I have no clue to why this is missing but if you want to skip initial throws in the debugger you can start without debugging first and then "attach" to it. Like so:
node --inspect --nolazy server/index.js
To attach to node with VSCode, make sure you have something like this in your .vscode\launch.json:
{
"name": "Docker: Attach to Node",
"type": "node",
"request": "attach",
"port": 9229,
"address": "localhost",
"localRoot": "${workspaceFolder}",
"remoteRoot": "/",
"protocol": "inspector"
}
Many node modules test things at startup by throwing and catching. For example:
var hasStacks = false;
try {
throw new Error();
} catch (e) {
hasStacks = !!e.stack;
}
...and this leads to break in VS Code debugger. So you might want to attach (i.e. press run "Docker: Attach to Node" after a few seconds.

Error connecting to the Mongo database. bad auth Authentication failed heroku + atlas mongodb + strapi

I might not have enough code here to provide.
I am currently using strapi and running my app on heroku + atlas mongodb. Everything was working fine when I was running the app with postgresql by heroku but I am migrating everything to atlas mongodb instead.
in strapi I have my config as
{
"defaultConnection": "default",
"connections": {
"default": {
"connector": "strapi-hook-mongoose",
"settings": {
"client": "mongo",
"host": "${process.env.DATABASE_HOST}",
"port": "${process.env.DATABASE_PORT}",
"database": "${process.env.DATABASE_NAME}",
"username": "${process.env.DATABASE_USERNAME}",
"password": "${process.env.DATABASE_PASSWORD}"
},
"options": {
"useNullAsDefault": true,
"ssl": "${process.env.DATABASE_SSL}"
}
}
}
}
these configs are in .env which if running locally the file would look like
DATABASE_HOST=xxxxxxx-shard-00-00-xxxxxx.mongodb.net:25758,xxxxxxx-shard-00-01-xxxxxx.mongodb.net:25758,xxxxxxx-shard-00-02-xxxxxx.mongodb.net:25758/test?ssl=true&replicaSet=xxxxxxx-shard-0&authSource=admin&retryWrites=true
DATABASE_PORT=25758
DATABASE_USERNAME=xxxxx
DATABASE_PASSWORD=xxxxx
DATABASE_NAME=xxxxx
DATABASE_SSL=true
if I run it locally, it works perfectly and able to connect to DB.
so I deployed to heroku and went to heroku's setting to config vars. I configured the same vars as what I have in the .env file
I also checked atlas mongodb's ip whitelist, I whitelist to all traffics (any ip) so this shouldn't be the problem.
But as mentioned, I used the same .env locally and config vars on heroku....but on heroku I got this error message
error Error connecting to the Mongo database. bad auth Authentication failed.
Anyone has suggestions on the reason authentication would fail?
Thanks in advance for any suggestions and help.
DATABASE_SSL=true Environment Variables don't support boolean values, meaning it's passing that as a string. For now you will need to manually set that value in your config and not pass it over env in heroku.

Set env var for Node JS when launching through VS Code

I have Node JS Azure function that when I run locally needs the NODE_TLS_REJECT_UNAUTHORIZED=0 environment variable needs setting for my code to work. This is because I am connecting to an emulated Cosmos DB running locally on my machine which require Node to allow for self signed certificates.
I only want this environment variable setting when I run locally as in production I will be using a real (i.e. non emulated) Cosmos DB instance. Rather than putting and #if debug in my code (or the equivalent for a Node Azure Function) I'd like my VS Code project to set the env var when it is launched.
I've trying following this answers advice but when VS Code launches the program the environment variable is not set as I get a runtime error in my code about self signed certificates not being authorised. My launch.json looks like this:
{
"version": "0.2.0",
"configurations": [
{
"name": "Attach to Node Functions",
"type": "node",
"request": "attach",
"port": 9229,
"preLaunchTask": "func: ",
},
],
"environment": [{
"name": "NODE_TLS_REJECT_UNAUTHORIZED",
"value": "0"
}],
}
If I set the env var directly in code using process.env.NODE_TLS_REJECT_UNAUTHORIZED = 0 everything is fine. I don't know how I can get VS Code to set this, or if it is even possible.
Please update
"environment": [{
"name": "NODE_TLS_REJECT_UNAUTHORIZED",
"value": "0"
}],
To Be
"env": {
"name": "NODE_TLS_REJECT_UNAUTHORIZED",
"value": "0"
},
Also please check the alternatives.
You can Update package.json scripts to add all environment variables on start like "runLocal": "NODE_TLS_REJECT_UNAUTHORIZED=0 ... your start code " or You can use third party lib called dotenv
this is how it works
01- Create .env file
02- Write all your environment variables key=val NODE_TLS_REJECT_UNAUTHORIZED=0
03- Update package.json scripts to add "runLocal": "NODE_ENV=dev ... your start code "
04- Check if node NODE_ENV is equal dev then load the dotenv
05- Import module in case of node env is local and call config function require('dotenv').config()

pm2 config json gives 502 Bad gateway

I am using pm2 for my node server. My pm2 config json is as follows
{
"apps": [{
"name": "hello",
"script": "server/app.js",
"exec_mode": "fork",
"env": {
"NODE_ENV": "development",
"PORT": 9000
}
}]
}
I am starting the application using: pm2 start pm2Config.json
My application is showing online but not loading. When I try to access the application, it is giving me 502 bad gateway error. But nothing is coming in the logs.
I don't know what went wrong. Please help.
Thanks in advance.

Resources