2

Below is the code from package.json file

"scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start",
},

Below is my next.config.js file, here console.log always undefined

require("dotenv").config();
console.log(process.env.BASE_URL)
module.exports = {
  env: {
    API_BASE_URL: process.env.BASE_URL
  },
  reactStrictMode: true,
}

And this is in the .env.development

BASE_URL: 'http://localhost:3000'

When I ran the npm run dev command, it prints on terminal "Loaded env from /var/www/html/next/next-SC/.env.development"

So, why the console prints undefined always.

I'm using next js version "10.0.4"

3
  • Because file nextjs config would be loaded before env file loaded. Commented Jan 14, 2021 at 10:20
  • 1
    Shouldn't the env var in your .env.development be set like BASE_URL=http://localhost:3000? Commented Jan 14, 2021 at 11:43
  • In my Ubuntu server, I just set different environment variable in my local and server. They are fetched automatically. Just need to remember that, if you want to expose the environment variable to public, you have to prefix, NEXT_PUBLIC Commented Jan 14, 2021 at 12:54

3 Answers 3

1

I assume you are using React with nextjs. If not, then please disregard this answer. I am doing the same thing. React has built in support for env vars. All you need to do is to prefix REACT_APP to your environment vars. So, in your .env.development or .env.staging, etc., you can have REACT_APP_BASE_URL=https://blah.com. You can then access them in your app using process.env.REACT_APP_BASE_URL. Then to build based on environment, I have (I am using craco, you would just use your normal build command)

"build:nightly": "env-cmd -f .env.nightly craco build",
"build:nightly": "env-cmd -f .env.staging craco build",
"build:nightly": "env-cmd -f .env.beta craco build",
...
Sign up to request clarification or add additional context in comments.

Comments

0

For development environment, name the file .env.development, for production .env.production.

Next.js has built-in loader for environment variables. So dotenv or similar packages aren't needed. Just add the files. It will be loaded automatically (see documentation).

Comments

-1

in next.config.js

const { resolve,join } = require('path');
const {config} = require('dotenv');
let path=resolve('../')
config({ path: join(path,'.env') })
let MAIN_URL=process.env.NEXT_PUBLIC_BASE_URL_MAIN
console.log("MAIN_URL",MAIN_URL)
const nextConfig = {
  reactStrictMode: true,
  env: {
    API_BASE_URL: MAIN_URL
  },
}

module.exports = nextConfig

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.