How to pass pm2 env variables into the node repl? - node.js

Imagine that I have some pm2 configuration file with some env variables into it:
"env" : {
"NODE_ENV": "development",
"MONGO_ENABLED" : true,
"MONGO_URI": "mongodb://localhost:27017/cindx-dev",
},
How can I run the node REPL, so all this environment variables will be enabled?
Thanks in advance!

I'm going to guess they use this code, if not some other code to load that.
https://github.com/Unitech/pm2/blob/91786108d71b3fc6c182750c09b494619e28b28a/lib/ProcessContainer.js#L16
// Load all env-vars from master.
var pm2_env = JSON.parse(process.env.pm2_env);
for(var k in pm2_env) {
process.env[k] = pm2_env[k];
}
So for you just run something like
var obj={
"NODE_ENV": "development",
"MONGO_ENABLED" : true,
"MONGO_URI": "mongodb://localhost:27017/cindx-dev",
};
Object.keys(obj).forEach(k=>process.env[k]=obj[k])

Related

How to override ava test runner's env variables while testing?

Ava test runner in my project has its own env variables defined like this in package.json:
"ava": {
"require": [
"esm"
],
"files": [
"test/unit/**/**/ava-*"
],
"environmentVariables": {
"SERVER_DEFAULT_TIMEZONE": "Australia/Sydney",
"PAY_MAX": "false",
"NODE_ENV": "development"
},
"timeout": "20s"
},
Now when doing a unit test for a certain file it happens that I need to cover this line in the function that I'm testing:
const isPayMax = process.env.PAY_MAX === "true";
So I'm trying to make that condition true so I can cover in my test. However, no matter I tried setting it to "true" or any other value, it still keep the value defined in package.json.
I've also tried using rewire module but it doesn't seem to work as well.
Also tried setting manually the environment variable in my test file like this process.env.PAY_MAX = "true" but no luck as well.
Any thoughts on how to deal with this? Thanks.

Nestjs: Repl with monorepo mode

I have a nest app that is using monorepo mode. I would like to take advantage of the new repl feature that was released in nest 9.0+.
My directory structure looks as such:
apps/
--inventory-ops/src/app.module
--ticket-office/src/app.module
I have followed the instructions found in the docs creating a repl.ts, but when I run the repl commannd:
npm run start -- --entryFile repl
I get this error output:
Error: Cannot find module '/dist/apps/ticket-office/repl'
Looking at my dist folder, the only build target is main.js, which would explain it not being able to find the repl module. Do I need to update something in my webpack config to make sure repl.ts gets built as well? Any help would be appreciated.
I managed to solve this by adding a new project in nest-cli.json, for example:
{
"$schema": "https://json.schemastore.org/nest-cli",
"collection": "#nestjs/schematics",
"sourceRoot": "apps/gateway/src",
"monorepo": true,
"root": "apps/gateway",
"compilerOptions": {
"webpack": true,
"tsConfigPath": "apps/gateway/tsconfig.app.json"
},
"projects": {
"gateway": {
"type": "application",
"root": "apps/gateway",
"entryFile": "main",
"sourceRoot": "apps/gateway/src",
"compilerOptions": {
"tsConfigPath": "apps/gateway/tsconfig.app.json"
}
},
"ticket-office": {
"type": "application",
"root": "apps/ticket-office",
"entryFile": "main",
"sourceRoot": "apps/ticket-office/src",
"compilerOptions": {
"tsConfigPath": "apps/ticket-office/tsconfig.app.json"
}
},
"ticket-office:repl": { // <<--- HERE
"type": "application",
"root": "apps/ticket-office",
"entryFile": "repl", // <<-- HERE
"sourceRoot": "apps/ticket-office/src",
"compilerOptions": {
"tsConfigPath": "apps/ticket-office/tsconfig.app.json"
}
},
}
}
Then you can run nest start ticket-office:repl
I hope this helps.
EDIT:
Adapted the answer to your question.
Try to run this:
nest start <your-app> --config nest-cli.json --debug --watch -- --entryFile repl
I faced the same issue and this worked for me.
I don't really know why NestJS take repl file into consideration for building only when explicitly the cli config is provided. It's probably a bug with the CLI.
Alternative
Also, you can add a custom parameter to your command and start the REPL mode conditionally:
script:
nest start <your-app> --watch repl
main.ts file:
async function bootstrap() {
if (process.argv[process.argv.length - 1] === 'repl') {
return repl(AppModule);
}
// Non REPL mode Nest app initialisation
...
}
bootstrap();

How to access ecs task definition environment variables in nodejs?

I am trying to access the environment variables set in a task definition, inside my nodejs app, with process.env.
I use a Dockerfile to create an image of the project, upload it to ECR, then use this image in the task definition.
I set enviroment variables for the nodejs app, inside the Dockerfile, like this:
# Dockerfile
...
RUN ROOT_DIR='/'
RUN PUBLIC_DIR='/public'
...
I have this task definition:
# task_definition.json
...
"environment" : [
{ "name" : "KeyOne", "value" : "KeyOneValue" },
{ "name" : "KeyTwo", "value" : "KeyTwoValue" }
]
...
I am not able to access process.env.KeyOne / process.env.KeyTwo (they are undefined)
I would like to be able to set those environment variables from the task definition and then reference them inside nodejs app with process.env instead of setting them inside the Dockerfile.
Here is a test I just made on my account, using ECS Fargate. All env variables from the task definition are accessible from NodeJS code.
Source Code is at
https://github.com/sebsto/ecs-demo/tree/master/so
TaskDefinition excerpt :
"environment": [
{
"name": "KEY1",
"value": "VALUE1"
}
],
Code extract :
app.get('/', (req, res) => {
res.send(`Hello world<br/>${JSON.stringify(process.env, null, 2)}`);
});
Output :
Hello world
{ "KEY1": "VALUE1", "NODE_VERSION": "10.16.0", "HOSTNAME": "ip-10-0-0-83.eu-west-1.compute.internal", "YARN_VERSION": "1.16.0", "HOME": "/root", "AWS_CONTAINER_CREDENTIALS_RELATIVE_URI": "/v2/credentials/b630982f-dffb-4ccc-9c8b-8311e42b57ab", "AWS_EXECUTION_ENV": "AWS_ECS_FARGATE", "AWS_DEFAULT_REGION": "eu-west-1", "ECS_CONTAINER_METADATA_URI": "http://169.254.170.2/v3/12186f93-de7b-47e3-a096-b0f23d7e0e81", "PATH": "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", "AWS_REGION": "eu-west-1", "PWD": "/usr/src/app" }
I'll keep the container is up and running for a few months, you can test yourself at http://52.18.232.75:8080/

process.env variable undefined on test using Jest in Azure function(Nodejs)

process.env variable showing undefined while running yarn run jest. I have stored every config variables in my local.settings.json file, and it is working perfectly in normal debug.
My code is like this:
local.settings.json:
{
"IsEncrypted": false,
"Host": {
"LocalHttpPort": 7071,
"CORS": "*"
},
"Values": {
"IOTHUB_CONNECTION_STRING": "xxxxxxxxxxxxxxxxxx",
"AZURE_DEVOPS_URL": "xxxxxxxxxxxxxxxxxxxx"
}
}
jest.config.js:
module.exports = {
log: jest.fn(),
}
azure httptrigger function - index.js:
const iothubConnectionString = process.env["IOTHUB_CONNECTION_STRING"];
console.log( iothubConnectionString ) //undefined
My tests fail due to these configuration variables. I need to pass the test. anyone can you please help me.
local.settings.json is specific to Azure Functions Core Tools. As long as you use Core Tools CLI to run the function you can access environment variables defined in this file via process.env

Running several scripts with forever

I have several scripts in a directory, each of the scripts called bot and it's number, from 1 to the number of the scripts.
What I would like to do is somehow run all of the scripts by 1 command line through the terminal (Using Ubuntu), I've used forever command to run the script without stopping and etc.
Could you make it through the terminal or using a node js script?
Is there any other commands like forever that would do it for me?
You could use it through the command line with the command forever.
You'll need to create a JSON file with the files you need.
Example:
[
{
// App1
"uid": "app1", // ID of the script.
"append": true,
"watch": true,
"script": "bot1.js", // Name of the script
"sourceDir": "" // Where the script is located. If it's in the
// same location as the json file, leave it ""
},
{
// App2 = > Same as app1, just different script name.
"uid": "app2",
"append": true,
"watch": true,
"script": "bot2.js",
"sourceDir": ""
}
]
Then you need just to run the JSON file through the forever command.
Example:
forever start apps.json
You can see more information about forever here.
My answer is the same as the answer by #Nikita Ivanov but with pm2. I personally like pm2, which also uses a config file just like forever, but it can be a js, json or yaml file.
// JS File
module.exports = {
apps : [{
name: "bot1",
script: "./bot1.js",
watch: true, // some optional param just for example
env: {
"NODE_ENV": "development",
}, // some optional param just for example
env_production : {
"NODE_ENV": "production"
} // some optional param just for example
},{
name: "bot2",
script: "./bot2.js",
instances: 4, // some optional param just for example
exec_mode: "cluster" // some optional param just for example
}]
}
Now if you do not know the number of scripts there are, it ok. Since it is JS, you can write a script to get the list of all the files in the directory and create an array similar to the one above and use that config for pm2.
module.exports = (function () {
// logic to get all file names and create the 'apps' array
return {
apps: apps
}
})()
Furthermore, you can also use the pm2 npm module and use pm2 as a module in a js script and do this.
See PM2 DOCS for more info.

Resources