Build error occurred, TypeError: Cannot convert undefined or null to object
See original GitHub issueBug report
Describe the bug
I’m trying to deploy nextjs app by now. On build step it throws error. What is wired is that when I’m trying to build the app on my local machine, everything goes well. So the error only happens while now tries to build it.
Error
This is the log file generated by now.
2019-12-15T09:40:53.669Z [4/4] Building fresh packages...
2019-12-15T09:40:56.994Z Done in 32.35s.
2019-12-15T09:40:57.027Z Running "yarn run build"
2019-12-15T09:40:57.366Z yarn run v1.17.3
2019-12-15T09:40:57.404Z $ next build
2019-12-15T09:40:58.000Z undefined
2019-12-15T09:40:58.002Z Creating an optimized production build...
2019-12-15T09:40:58.038Z Attention: Next.js now collects completely anonymous telemetry regarding usage.
2019-12-15T09:40:58.038Z This information is used to shape Next.js' roadmap and prioritize features.
2019-12-15T09:40:58.038Z You can learn more, including how to opt-out if you'd not like to participate in this anonymous program, by visiting the following URL:
2019-12-15T09:40:58.038Z https://nextjs.org/telemetry
2019-12-15T09:40:58.038Z
2019-12-15T09:40:58.073Z > Build error occurred
2019-12-15T09:40:58.074Z TypeError: Cannot convert undefined or null to object
2019-12-15T09:40:58.074Z at Function.keys (<anonymous>)
2019-12-15T09:40:58.074Z at getBaseWebpackConfig (/zeit/58b8b8a2/node_modules/next/dist/build/webpack-config.js:116:78)
2019-12-15T09:40:58.074Z at build (/zeit/58b8b8a2/node_modules/next/dist/build/index.js:5:887)
2019-12-15T09:40:58.094Z error Command failed with exit code 1.
2019-12-15T09:40:58.094Z info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
2019-12-15T09:40:58.109Z Error: Exited with 1
2019-12-15T09:40:58.109Z at ChildProcess.child.on (/zeit/9183b56f30936552/.build-utils/.builder/node_modules/@now/next/dist/index.js:49423:24)
2019-12-15T09:40:58.109Z at emitTwo (events.js:126:13)
2019-12-15T09:40:58.109Z at ChildProcess.emit (events.js:214:7)
2019-12-15T09:40:58.109Z at maybeClose (internal/child_process.js:925:16)
2019-12-15T09:40:58.109Z at Process.ChildProcess._handle.onexit (internal/child_process.js:209:5)
2019-12-15T09:40:58.229Z worker exited with code 20 and signal null
2019-12-15T09:41:00.627Z done
I found the line the error is mentioning:
at getBaseWebpackConfig (/zeit/448bf18e/node_modules/next/dist/build/webpack-config.js:116:78)
In webpack-config.js
line 116
, There is a code to handle env variables I guess ...Object.keys(config.env).reduce((acc,key)
. I believe this line causes the error (I’m not sure).
I have environment variables defined in .env
file:
API_HOST=https://somedomain.com
API_PORT=443
And in next.config.js
I read it like below via dotenv
package.
const dotenv = require('dotenv').config();
const withSass = require('@zeit/next-sass');
const withCSS = require('@zeit/next-css');
module.exports = withCSS(
withSass({
env: dotenv.parsed
})
);
Error Screenshot
System information
- OS: Ubuntu 18.04 LTS
- Version of Next.js: 9.1.5
- Version of now: 16.7.0
- Version of Nodejs: 13.3.0
Just I want to mention again that the whole build process is working when I’m trying to build with command
yarn build
which is equal tonext build
on my local machine.
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (4 by maintainers)
Hello, have you been able to work out a build with a .env file or to figure out another solution instead ?
We are having the same problem building our front inside a docker container using next.js and dotenv.
edit:
We managed to make it working, don’t use .parsed :
use it like this :
This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.