Next.js: Build error: TypeError: Cannot read property 'replace' of undefined

Created on 7 Nov 2019  路  2Comments  路  Source: vercel/next.js

Bug report

Describe the bug

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

To Reproduce

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;

Expected behavior

I would expect no build errors and ability to use process.env variables across the code.

Screenshots

If applicable, add screenshots to help explain your problem.

System information

  • Version of Next.js: 9.1.1

Additional context

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)

Most helpful comment

@kanil4 how did you solve it? I'm having the same problem.

All 2 comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

irrigator picture irrigator  路  3Comments

pie6k picture pie6k  路  3Comments

ghost picture ghost  路  3Comments

knipferrc picture knipferrc  路  3Comments

timneutkens picture timneutkens  路  3Comments