Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When using "type": "module" import does not interop the default export for commonjs modules #34412

Closed
timneutkens opened this issue Feb 16, 2022 · 3 comments · Fixed by #35933 or #36082
Assignees

Comments

@timneutkens
Copy link
Member

Run next info (available from version 12.0.8 and up)

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 21.0.1: Tue Sep 14 20:56:24 PDT 2021; root:xnu-8019.30.61~4/RELEASE_ARM64_T6000
Binaries:
  Node: 16.13.0
  npm: 8.1.2
  Yarn: 1.22.17
  pnpm: 6.24.4
Relevant packages:
  next: 12.0.11-canary.17
  react: 17.0.2
  react-dom: 17.0.2

What version of Next.js are you using?

12.0.11-canary.17

What version of Node.js are you using?

v16.13.0

What browser are you using?

Chrome

What operating system are you using?

macOS

How are you deploying your application?

Affects all platforms

Describe the Bug

When using next/link, next/image, or any other module that is not real ESM but has a default export the code importing the module does not automatically use interopRequireDefault which turns the default import into an object holding default and any other exports from the module. Example:

import Head from "next/head";
import Image from "next/image"

console.log({Head, Image})

Result from above code:

{
  Head: { defaultHead: [Function: defaultHead], default: [Function: Head] },
  Image: { default: [Function: Image] }
}

This results in the build error below:

Build error

info - Checking validity of types
info - Creating an optimized production build
info - Compiled successfully
info - Collecting page data
[ ] info - Generating static pages (0/3)
Error occurred prerendering page "/". Read more: https://nextjs.org/docs/messages/prerender-error
Error: Minified React error #130; visit https://reactjs.org/docs/error-decoder.html?invariant=130&args[]=object&args[]= for the full message or use the non-minified dev environment for full errors and additional helpful warnings.
at a.b.render (/Users/timneutkens/projects/sandbox/kfjkdsfjkjdkfsjkd/node_modules/react-dom/cjs/react-dom-server.node.production.min.js:45:32)
at a.b.read (/Users/timneutkens/projects/sandbox/kfjkdsfjkjdkfsjkd/node_modules/react-dom/cjs/react-dom-server.node.production.min.js:41:83)
at Object.exports.renderToString (/Users/timneutkens/projects/sandbox/kfjkdsfjkjdkfsjkd/node_modules/react-dom/cjs/react-dom-server.node.production.min.js:52:138)
at Object.renderPage (/Users/timneutkens/projects/sandbox/kfjkdsfjkjdkfsjkd/node_modules/next/dist/server/render.js:749:45)
at Object.defaultGetInitialProps (/Users/timneutkens/projects/sandbox/kfjkdsfjkjdkfsjkd/node_modules/next/dist/server/render.js:387:51)
at Function.getInitialProps (/Users/timneutkens/projects/sandbox/kfjkdsfjkjdkfsjkd/.next/server/pages/_document.js:530:20)
at Object.loadGetInitialProps (/Users/timneutkens/projects/sandbox/kfjkdsfjkjdkfsjkd/node_modules/next/dist/shared/lib/utils.js:69:29)
at renderDocument (/Users/timneutkens/projects/sandbox/kfjkdsfjkjdkfsjkd/node_modules/next/dist/server/render.js:762:48)
at renderToHTML (/Users/timneutkens/projects/sandbox/kfjkdsfjkjdkfsjkd/node_modules/next/dist/server/render.js:826:34)
at async /Users/timneutkens/projects/sandbox/kfjkdsfjkjdkfsjkd/node_modules/next/dist/export/worker.js:269:36
info - Generating static pages (3/3)

Build error occurred
Error: Export encountered errors on following paths:
/
at /Users/timneutkens/projects/sandbox/kfjkdsfjkjdkfsjkd/node_modules/next/dist/export/index.js:498:19
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async Span.traceAsyncFn (/Users/timneutkens/projects/sandbox/kfjkdsfjkjdkfsjkd/node_modules/next/dist/trace/trace.js:75:20)
at async /Users/timneutkens/projects/sandbox/kfjkdsfjkjdkfsjkd/node_modules/next/dist/build/index.js:1025:17
at async Span.traceAsyncFn (/Users/timneutkens/projects/sandbox/kfjkdsfjkjdkfsjkd/node_modules/next/dist/trace/trace.js:75:20)
at async /Users/timneutkens/projects/sandbox/kfjkdsfjkjdkfsjkd/node_modules/next/dist/build/index.js:899:13
at async Span.traceAsyncFn (/Users/timneutkens/projects/sandbox/kfjkdsfjkjdkfsjkd/node_modules/next/dist/trace/trace.js:75:20)
at async Object.build [as default] (/Users/timneutkens/projects/sandbox/kfjkdsfjkjdkfsjkd/node_modules/next/dist/build/index.js:82:25)
error Command failed with exit code 1.

Expected Behavior

I need to check this one with @sokra given that there's two ways we could go with this. Will circle back on this issue once I've done so.

To Reproduce

  • yarn create next-app module-issue && cd module-issue && yarn add next@canary
  • Open package.json and add "type": "module" to the top
  • npm run build
@JTaylor0196
Copy link
Contributor

JTaylor0196 commented Mar 23, 2022

@timneutkens Hey!

Just curious if there Is there currently a timeline for when this'll be finished/closed? Thanks!

@jeremytenjo
Copy link

Getting error when calling an api route when my package.json type=module

2022-04-01T13:54:35.635Z undefined ERROR Uncaught Exception {"errorType":"ReferenceError","errorMessage":"exports is not defined in ES module scope\nThis file is being treated as an ES module because it has a '.js' file extension and '/var/task/package.json' contains \"type\": \"module\". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.","stack":["ReferenceError: exports is not defined in ES module scope","This file is being treated as an ES module because it has a '.js' file extension and '/var/task/package.json' contains \"type\": \"module\". To treat it as a CommonJS script, rename it to use the '.cjs' file extension."," at file:///var/task/___next_launcher.js:2:23"," at ModuleJob.run (internal/modules/esm/module_job.js:183:25)"," at process.runNextTicks [as _tickCallback] (internal/process/task_queues.js:60:5)"," at /var/runtime/deasync.js:23:15"," at _tryAwaitImport (/var/runtime/UserFunction.js:74:12)"," at _tryRequire (/var/runtime/UserFunction.js:162:21)"," at _loadUserApp (/var/runtime/UserFunction.js:197:12)"," at Object.module.exports.load (/var/runtime/UserFunction.js:242:17)"," at Object.<anonymous> (/var/runtime/index.js:43:30)"," at Module._compile (internal/modules/cjs/loader.js:1085:14)"]} Unknown application error occurred ReferenceError

@Brooooooklyn Brooooooklyn reopened this Apr 12, 2022
@kodiakhq kodiakhq bot closed this as completed in #36082 Apr 12, 2022
kodiakhq bot pushed a commit that referenced this issue Apr 12, 2022
fixes #34412

## Bug

- [x] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `yarn lint`
@github-actions
Copy link
Contributor

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 13, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
4 participants