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

  • February 7, 2025
  • 3 replies
  • 882 views

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 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by sarav_prakash

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.

3 replies

AMANATH_ULLAH
Community Advisor
Community Advisor
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/ 

Amanath Ullah
sarav_prakash
Community Advisor
Community Advisor
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. 

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?
sarav_prakash
Community Advisor
sarav_prakashCommunity AdvisorAccepted solution
Community Advisor
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.
kautuk_sahni
Community Manager
Community Manager
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!

Kautuk Sahni