Next.js: Build error occurred, TypeError: Cannot convert undefined or null to object

Created on 15 Dec 2019  路  4Comments  路  Source: vercel/next.js

Bug 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

error

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 to next build on my local machine.

good first issue

Most helpful comment

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 :

require("dotenv").config();

module.exports = {
  env: {
    MY_ENV_VAR: process.env.MY_ENV_VAR
  },
};

All 4 comments

I assume env: dotenv.parsed refers to null / undefined or something that passes the if check for env. Would probably make sense to add a message with err.sh link.

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 :

require("dotenv").config();

module.exports = {
  env: {
    MY_ENV_VAR: process.env.MY_ENV_VAR
  },
};
Was this page helpful?
0 / 5 - 0 ratings

Related issues

havefive picture havefive  路  3Comments

swrdfish picture swrdfish  路  3Comments

formula349 picture formula349  路  3Comments

YarivGilad picture YarivGilad  路  3Comments

knipferrc picture knipferrc  路  3Comments