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 fails with ERR_MODULE_NOT_FOUND - Works in 11.x but not in 12.x #31974

Closed
lionelhorn opened this issue Nov 30, 2021 · 5 comments
Closed
Assignees
Labels
bug Issue was opened via the bug report template. Upstream Related to using Next.js with a third-party dependency. (e.g., React, UI/icon libraries, etc.).

Comments

@lionelhorn
Copy link

What version of Next.js are you using?

12.0.4

What version of Node.js are you using?

17.0.1

What browser are you using?

Chrome

What operating system are you using?

Windows

How are you deploying your application?

next build

Describe the Bug

next build erros with a Module not found

info  - Creating an optimized production build  
info  - Compiled successfully

> Build error occurred
Error [ERR_MODULE_NOT_FOUND]: Cannot find module 'C:\PROG\Issues\issues-nextjs-deepkit\node_modules\@deepkit\type\dist\esm\src\types' imported from C:\PROG\Issues\issues-nextjs-deepkit\node_modules\@deepkit
\type\dist\esm\index.js
    at new NodeError (node:internal/errors:371:5)
    at finalizeResolution (node:internal/modules/esm/resolve:394:11)
    at moduleResolve (node:internal/modules/esm/resolve:915:10)
    at defaultResolve (node:internal/modules/esm/resolve:1005:11)
    at ESMLoader.resolve (node:internal/modules/esm/loader:475:30)
    at ESMLoader.getModuleJob (node:internal/modules/esm/loader:245:18)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:79:40)
    at link (node:internal/modules/esm/module_job:78:36) {
  type: 'Error',
  code: 'ERR_MODULE_NOT_FOUND'
}

Expected Behavior

A proper successful build.

To Reproduce

I created a repo with 2 branches and steps.

next 11.x branch : Building correctly
next 12.x branch : Building fails

https://github.com/lionelhorn/issues-nextjs-deepkit

@lionelhorn lionelhorn added the bug Issue was opened via the bug report template. label Nov 30, 2021
@lionelhorn lionelhorn changed the title ERR_MODULE_NOT_FOUND when building. Works in 11.x but not in 12.x next build fails with ERR_MODULE_NOT_FOUND - Works in 11.x but not in 12.x Nov 30, 2021
@lionelhorn lionelhorn changed the title next build fails with ERR_MODULE_NOT_FOUND - Works in 11.x but not in 12.x next build fails with ERR_MODULE_NOT_FOUND - Works in 11.x but not in 12.x Nov 30, 2021
@lionelhorn lionelhorn changed the title next build fails with ERR_MODULE_NOT_FOUND - Works in 11.x but not in 12.x next build fails with ERR_MODULE_NOT_FOUND - Works in 11.x but not in 12.x Nov 30, 2021
@lionelhorn lionelhorn changed the title next build fails with ERR_MODULE_NOT_FOUND - Works in 11.x but not in 12.x next build fails with ERR_MODULE_NOT_FOUND - Works in 11.x but not in 12.x Nov 30, 2021
@balazsorban44
Copy link
Member

balazsorban44 commented Nov 30, 2021

Adding this to next.config.js

experimental: {
  esmExternals: false,
},

Fixes this temporarily.

Seems to be related to https://nextjs.org/blog/next-12#es-modules-support-and-url-imports

I'll try to look more into this.

Here is a related thread: #27876

@balazsorban44 balazsorban44 self-assigned this Nov 30, 2021
@lionelhorn
Copy link
Author

Thanks @balazsorban44 for the info and links.
It fixes the initial issue :)

@balazsorban44
Copy link
Member

I looked at the module that is failing. @deepkit/type

Adding .js to the imports and exports in its files under dist/esm seems to help.

Might be because of: https://nodejs.org/api/esm.html#mandatory-file-extensions

In that case, maybe that dependency is packaged incorrectly and Next.js was good at masking the issue in previous versions but is more strict about it in newer ones? 🤔

@balazsorban44 balazsorban44 added the Upstream Related to using Next.js with a third-party dependency. (e.g., React, UI/icon libraries, etc.). label Nov 30, 2021
@balazsorban44
Copy link
Member

balazsorban44 commented Nov 30, 2021

So yes. My assumption seems to be correct. This applies: https://nodejs.org/api/esm.html#mandatory-file-extensions

Next.js 12 defaults esmExternals to true. That means it will treat node_modules as ESM instead of Common.js, when available. node_modules are directly loaded by Node.js and not bundled, so the Node.js ESM guidelines apply.

This means you will have to take it up with the @deepkit developers so they can fix their package distribution accordingly.

@balazsorban44
Copy link
Member

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.

@vercel vercel locked as resolved and limited conversation to collaborators Jan 27, 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. Upstream Related to using Next.js with a third-party dependency. (e.g., React, UI/icon libraries, etc.).
Projects
None yet
Development

No branches or pull requests

2 participants