Expand my Community achievements bar.

Not able to read the environment specific details

Avatar

Level 9

Hello Team,

 

I have created Adobe IO runtime project and trying to run in my local system. But, my logic is not picking the environment specific details.

Here is the .env file data

AIO_RUNTIME_AUTH=12345678

AIO_RUNTIME_NAMESPACE=123456-poc-testing

NODE_ENV=development

 

My node js file has

 

// Import the dotenv package to load environment variables
import dotenv from 'dotenv';
dotenv.config();

// Import default configuration
import defaultConfig from './default.js';

// Dynamically import the environment-specific configuration

  const env = process.env.NODE_ENV || 'development';
  console.log("end file is:",env,process.env.NODE_ENV);
  const envConfigModule = await import(`./${env}.js`);
  return envConfigModule.default;


// Export a function to get the merged configuration

  const envConfig = await loadEnvConfig();
  console.log("defaultConfig",defaultConfig)
  return { ...defaultConfig, ...envConfig };

 

Here, instead of giving the data from development.js file, I am getting the value for production.js file

console.log("end file is:",env,process.env.NODE_ENV);  This line gives me production.

Thanks in advance.

cc @AMANATH_ULLAH  @sarav_prakash @mk_aem21 @arunpatidar  @AmitVishwakarma 

5 Replies

Avatar

Community Advisor

@Mahesh_Gunaje 

To read the namespace you can use below environment variable

process.env.__OW_NAMESPACE

https://developer.adobe.com/runtime/docs/guides/reference/environment_variables/ 


Amanath Ullah

Avatar

Level 8

You dont need dotenv from node20.6.0. Read here. https://nodejs.org/en/blog/release/v20.6.0#notable-changes

 

So to fix,

  1. open your package.json
  2. Check and make sure you run node20 like this  

 

"engines": {
    "node": "20.11.0"
  },​

 

  • Uninstall dotenv with `npm uninstall dotenv`
  • Remove these 2 lines, 
    import dotenv from 'dotenv';
    dotenv.config();
  • Node must load .env variables 

Another note: Even if you create .env.prod and .env.dev, when deploying you manually need to copy .env.dev into .env during dev deploy and similar for prod. Instead setup CD using github actions and populate .env using github secrets. Thereby deployment pipeline will automatically populate right secrets. 

 

I am guessing, when you are deploying into dev, your .env is having .env.prod values. Thats why you see incorrect. Ensure your .env is populated correctly during dev deploy. 

Avatar

Level 9

Thanks @sarav_prakash  for your help.

Now, my package.json file has this details: 

{
  "name": "testProj",
  "version": "0.0.1",
  "engine":{"node":">=20"},
   "dependencies": {
 
Also, removed these lines from the project
import dotenv from 'dotenv';
dotenv.config();
 
To access the env file
const env = process.env.NODE_ENV || 'development'; 
console.log("values is:",env,process.env.NODE_ENV);
Is the right way to access  NODE_ENV?

Avatar

Level 8

preferably use single node version as 

"engines": {
"node": "20.11.0"
},
 
Second, when deploying and testing from dev, make sure you update the .env with dev values and deploy. Its right, it should work.

Avatar

Administrator

@Mahesh_Gunaje Did you find the suggestions helpful? Please let us know if you need more information. If a response worked, kindly mark it as correct for posterity; alternatively, if you found a solution yourself, we’d appreciate it if you could share it with the community. Thank you!



Kautuk Sahni