Not able to read the environment specific details | Community
Skip to main content
Level 7
February 7, 2025
Solved

Not able to read the environment specific details

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 

sarav_prakash
sarav_prakashAccepted solution
New Participant
February 10, 2025

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.

5 replies

kautuk_sahni
Employee
February 10, 2025

@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!

Translate
Kautuk Sahni
sarav_prakash
New Participant
February 10, 2025

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. 

Translate
Level 7
February 10, 2025

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?
Translate
sarav_prakash
sarav_prakashAccepted solution
New Participant
February 10, 2025

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.
Translate
AMANATH_ULLAH
New Participant
February 7, 2025

@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/ 

Translate
Amanath Ullah