Keep getting an error as Cannot read property 'replace' of undefined while running npm run build on my nextJS project. I am following the nextJS documentation to configure env variables for public runtime config. -> https://nextjs.org/docs#runtime-configuration
Here is how i defined the env variables in next.config.js ->
module.exports = {
env: {
APP_SERVER_URL: process.env.APP_SERVER_URL
}
};
Here is how i am trying tot use it:
import getConfig from 'next/config';
const { publicRuntimeConfig } = getConfig();
const httpUri = publicRuntimeConfig.APP_SERVER_URL;
I would expect no build errors and ability to use process.env variables across the code.
If applicable, add screenshots to help explain your problem.
Full error ->
Build error occurred
TypeError: Cannot read property 'replace' of undefined
at Module.1TCz (/Users/Documents/SHIFT/create/eco-ui-ssr/src/.next/server/static/azPtrXenFyoDdfct9cPkm/pages/_app.js:408:23)
at __webpack_require__ (/Users/Documents/SHIFT/create/eco-ui-ssr/src/.next/server/static/azPtrXenFyoDdfct9cPkm/pages/_app.js:23:31)
at Object.0 (/Users/Documents/SHIFT/create/eco-ui-ssr/src/.next/server/static/azPtrXenFyoDdfct9cPkm/pages/_app.js:147:18)
at __webpack_require__ (/Users/Documents/SHIFT/create/eco-ui-ssr/src/.next/server/static/azPtrXenFyoDdfct9cPkm/pages/_app.js:23:31)
at /Users/Documents/SHIFT/create/eco-ui-ssr/src/.next/server/static/azPtrXenFyoDdfct9cPkm/pages/_app.js:91:18
at Object.(/Users/Documents/SHIFT/create/eco-ui-ssr/src/.next/server/static/azPtrXenFyoDdfct9cPkm/pages/_app.js:94:10)
at Module._compile (internal/modules/cjs/loader.js:776:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
Hi, the env config exposes the value by replacing any places you write process.env. APP_SERVER_URL with the value in your next.config.js, you don't need to use next/config as that's separate. Updating the above httpUri variable to const httpUri = process.env.APP_SERVER_URL should resolve the above problem.
I'm going to close this since it doesn't appear to be a bug. If you are still having trouble feel free to reply with additional details or see env docs here for more info
@kanil4 how did you solve it? I'm having the same problem.
Most helpful comment
@kanil4 how did you solve it? I'm having the same problem.