question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
Ex4lcommented, Feb 12, 2020

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
  },
};
0reactions
balazsorban44commented, Jan 30, 2022

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cannot convert undefined or null to object - Stack Overflow
This error is caused when you call a function that expects an Object as its argument, but pass undefined or null instead, like...
Read more >
Cannot convert undefined or null to Object in JavaScript
The "Cannot convert undefined or null to Object" error occurs when we pass a null or an undefined value to a function that...
Read more >
Cannot convert undefined or null to object" with next dev ...
Running dev with webpack 5 results in this error: ... Build error occurred, TypeError: Cannot convert undefined or null to object #9754 ......
Read more >
typeerror: cannot convert undefined or null to object - You.com
Solve The Error “TypeError: Cannot convert undefined or null to object” Solution 1: Check if the value is null or undefined before operating...
Read more >
Cannot convert undefined or null to object : r/learnjavascript
Your code throws the error because before fetch completes, location is undefined, but you are telling react to iterate over it, which can't...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found