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-781] strict mode error for projects that do not specify "type": "module" #34796

Open
1 task done
sangotaro opened this issue Feb 25, 2022 · 1 comment
Open
1 task done
Labels
bug Issue was opened via the bug report template. SWC Related to minification/transpilation in Next.js.

Comments

@sangotaro
Copy link
Contributor

sangotaro commented Feb 25, 2022

Verify canary release

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

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 20.6.0: Tue Oct 12 18:33:38 PDT 2021; root:xnu-7195.141.8~1/RELEASE_ARM64_T8101
Binaries:
  Node: 16.13.1
  npm: 8.1.2
  Yarn: N/A
  pnpm: N/A
Relevant packages:
  next: 12.1.0
  react: 17.0.2
  react-dom: 17.0.2

What browser are you using? (if relevant)

Chrome

How are you deploying your application? (if relevant)

No response

Describe the Bug

In next dev, Non-ESM files default to strict mode in projects that do not specify "type": "module" in package.json

Therefore, the non-ESM script is trapped by the strict mode validation and an error occurs.

// non-esm.js
with (Math) {
  console.log(PI);
}
// pages/index.tsx
import type { NextPage } from "next";
import { useEffect } from "react";

const Home: NextPage = () => {
  useEffect(() => {
    require("../non-esm.js"); // Error!!
  }, []);
  return <div>test</div>;
};

export default Home;

babel error:
スクリーンショット 2022-02-25 16 43 23

swc error:
スクリーンショット 2022-02-25 15 16 07

In the case of babel-loader, this problem can be avoided by changing the extension from js to cjs. However, for swc, this problem cannot be avoided with either extension.

This problem occurs at next@12.1.0. Possible causes of PR: #33637

Expected Behavior

next dev works without error. 

To Reproduce

NEXT-781

@sangotaro sangotaro added the bug Issue was opened via the bug report template. label Feb 25, 2022
@balazsorban44 balazsorban44 added the SWC Related to minification/transpilation in Next.js. label Mar 3, 2022
@jankaifer
Copy link
Contributor

jankaifer commented Jan 12, 2023

@github-actions github-actions bot added the linear: next Confirmed issue that is tracked by the Next.js team. label Jan 12, 2023
@balazsorban44 balazsorban44 added kind: bug and removed kind: bug linear: next Confirmed issue that is tracked by the Next.js team. labels Mar 6, 2023
@balazsorban44 balazsorban44 changed the title strict mode error for projects that do not specify "type": "module" [NEXT-781] strict mode error for projects that do not specify "type": "module" Mar 6, 2023
@samcx samcx removed the kind: bug label Apr 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issue was opened via the bug report template. SWC Related to minification/transpilation in Next.js.
Projects
None yet
Development

No branches or pull requests

4 participants