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 build failed to import web workers from pure esm packages #33914

Open
yume-chan opened this issue Feb 2, 2022 · 1 comment
Open

next build failed to import web workers from pure esm packages #33914

yume-chan opened this issue Feb 2, 2022 · 1 comment
Labels
bug Issue was opened via the bug report template. Webpack Related to Webpack with Next.js.

Comments

@yume-chan
Copy link

yume-chan commented Feb 2, 2022

Run next info (available from version 12.0.8 and up)

'yarn' is not recognized as an internal or external command,
operable program or batch file.
'pnpm' is not recognized as an internal or external command,
operable program or batch file.

    Operating System:
      Platform: win32
      Arch: x64
      Version: Windows 10 Pro
    Binaries:
      Node: 16.13.1
      npm: 8.1.2
      Yarn: N/A
      pnpm: N/A
    Relevant packages:
      next: 12.0.11-canary.1
      react: 17.0.2
      react-dom: 17.0.2

What version of Next.js are you using?

12.0.11-canary.1

What version of Node.js are you using?

v16.13.1

What browser are you using?

Unrelated

What operating system are you using?

Windows 11

How are you deploying your application?

Unrelated

Describe the Bug

When a dependency package has "type": "module" in package.json, and uses new Worker(new URL("./worker.js", import.meta.url)) to create a web worker, next build failed with

Module not found: ESM packages (./worker.js) need to be imported. Use 'import' to reference the package instead. https://nextjs.org/docs/messages/import-esm-externals

While next dev and a barebone webpack configuration all works.


This issue originated near here:

// When in esm externals mode, and using import, we resolve with
// ESM resolving options.
const isEsmRequested = dependencyType === 'esm'

dependencyType is "worker" in this case.

Expected Behavior

next build successfully create a build.

To Reproduce

git clone https://github.com/yume-chan/next-repro-33914
cd next-import-worker
npm i
cd packages/next-app
next build

The webpack-app folder is a barebone webpack configuration, which can correctly import and bundle the worker.

@yume-chan yume-chan added the bug Issue was opened via the bug report template. label Feb 2, 2022
@balazsorban44 balazsorban44 added Webpack Related to Webpack with Next.js. kind: bug and removed bug Issue was opened via the bug report template. labels Feb 11, 2022
@christoph-pflueger
Copy link

christoph-pflueger commented Jan 15, 2024

I'm experiencing a similar issue. In my case the error during next build is

> next build

   ▲ Next.js 14.0.4
   - Experiments (use at your own risk):
     · esmExternals

Failed to compile.

static/media/worker.1494bc9b.js from Terser
  x 'import', and 'export' cannot be used outside of module code
    ,-[38:1]
[...]

Caused by:
    0: failed to parse input file
    1: Syntax Error


> Build failed because of webpack errors
   Creating an optimized production build  . ELIFECYCLE  Command failed with exit code 1.

@samcx samcx added bug Issue was opened via the bug report template. and removed kind: bug labels 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. Webpack Related to Webpack with Next.js.
Projects
None yet
Development

No branches or pull requests

4 participants