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

Next 12.2 ReferenceError: exports is not defined #38117

Closed
1 task done
hanford opened this issue Jun 28, 2022 · 16 comments · Fixed by #38181
Closed
1 task done

Next 12.2 ReferenceError: exports is not defined #38117

hanford opened this issue Jun 28, 2022 · 16 comments · Fixed by #38181
Labels
bug Issue was opened via the bug report template.

Comments

@hanford
Copy link
Contributor

hanford commented Jun 28, 2022

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

Next 12.2(-12.2.2)

Describe the Bug

I'm no longer able to import code with exports properly in Next 12.2

Expected Behavior

Next should properly bundle these files

Link to reproduction

https://github.com/hanford/12.2-exports

To Reproduce

Pull reproduction repo and try running on Next 12.2.0, you'll see the following error:
image

You can check out the working branch, which just has Next 12.1.6, after installing the older version the code will run without error.

--

Happy to provide more information if it's helpful!

@hanford hanford added the bug Issue was opened via the bug report template. label Jun 28, 2022
@SukkaW
Copy link
Contributor

SukkaW commented Jun 29, 2022

@hanford Try to rename the problematic-file.js to problematic-file.cjs to see if it helps?

@hanford
Copy link
Contributor Author

hanford commented Jun 29, 2022

@SukkaW that triggers a different error:

Screen Shot 2022-06-28 at 7 58 19 PM

@SukkaW
Copy link
Contributor

SukkaW commented Jun 29, 2022

@hanford The issue is caused by you trying to mix ES Module and CommonJS files.

problematic-file.js is a CommonJS module that uses exports, while your pages/index.js is an ES Module that uses import/export.

@PizzaPete
Copy link
Contributor

We're running into the same thing. It was working before on version 12.1.6.

@rodilo
Copy link

rodilo commented Jun 29, 2022

Same here, was working fine with previous version.

@wweaver
Copy link

wweaver commented Jul 1, 2022

We fixed it by changing exports.foo = 'bar'; to export const foo = 'bar';.

@wiredmatt
Copy link

Can confirm that this was working before 12.2, we use next-transpile-modules to bring in mixed modules and they worked just fine up until 12.2

@pveyes
Copy link
Contributor

pveyes commented Jul 5, 2022

Tried bisecting the commit using https://github.com/hanford/12.2-exports as test and got this

❯ git bisect bad
ff0ccc48174a725e6af7cfb64294b51c878c412d is the first bad commit
commit ff0ccc48174a725e6af7cfb64294b51c878c412d
Author: JJ Kasper <jj@jjsweb.site>
Date:   Tue Jun 7 10:59:05 2022 -0500

    Enable externalHelpers swc option (#37150)

    * Enable externalHelpers swc option

    * disable with pnp

 packages/next/build/swc/options.js |  1 +
 packages/next/package.json         |  1 +
 pnpm-lock.yaml                     | 16 ++++++++++++++--
 3 files changed, 16 insertions(+), 2 deletions(-)

Setting the externalHelpers (https://github.com/vercel/next.js/pull/37150/files#diff-96b89d2e52974b2193af9cc719e28794d42dc41a67f6abba7e5fbae4896a33acR54) to false fixes the issue

@rodilo
Copy link

rodilo commented Jul 5, 2022

Is there a way to set this thought next.config.js @pveyes ?

@pveyes
Copy link
Contributor

pveyes commented Jul 5, 2022

Nope, you can use patch-package if you want using this patch (patches/next+12.2.0.patch)

diff --git a/node_modules/next/dist/build/swc/options.js b/node_modules/next/dist/build/swc/options.js
index 4c650b2..d86fd41 100755
--- a/node_modules/next/dist/build/swc/options.js
+++ b/node_modules/next/dist/build/swc/options.js
@@ -44,7 +44,7 @@ function getBaseSWCOptions({ filename , jest , development , hasReactRefresh , g
                 baseUrl: resolvedBaseUrl,
                 paths
             } : {},
-            externalHelpers: !process.versions.pnp,
+            externalHelpers: false,
             parser: parserConfig,
             experimental: {
                 keepImportAssertions: true,

@kmsheng
Copy link

kmsheng commented Jul 5, 2022

Why does vercel change my js file ?

It adds "use strict" and undo my minification.

@weiguanu
Copy link

add .babelrc without changing any configuration
{ "presets": ["next/babel"], "plugins": [] }

@hanford
Copy link
Contributor Author

hanford commented Jul 12, 2022

@weiguanu having a .babelrc file disables SWC compilation which is much more performant

@kodiakhq kodiakhq bot closed this as completed in #38181 Jul 12, 2022
kodiakhq bot pushed a commit that referenced this issue Jul 12, 2022
## Bug

- [ ] 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 `pnpm lint`
- [ ] The examples guidelines are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing.md#adding-examples)

---

 - Closes #38117
@hanford
Copy link
Contributor Author

hanford commented Jul 12, 2022

Woot, excited for the next canary! 🖤

@axeldelafosse
Copy link

Yay! Thank you @kdy1!

@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 Aug 12, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue was opened via the bug report template.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants