Next.js: `next build` 失败并显示“模块未自行注册”。

创建于 2019-07-11  ·  55评论  ·  资料来源: vercel/next.js

错误报告

描述错误

升级到 Next 9 后, next build失败并出现以下错误。

{ Error: Module did not self-register.
    at Object.Module._extensions..node (internal/modules/cjs/loader.js:857:18)
    at Module.load (internal/modules/cjs/loader.js:685:32)
    at Function.Module._load (internal/modules/cjs/loader.js:620:12)
    at Module.require (internal/modules/cjs/loader.js:723:19)
    at require (internal/modules/cjs/helpers.js:14:16)
    at Object.<anonymous> (/home/vista1nik/Documents/nextjs-project/node_modules/grpc/src/grpc_extension.js:32:13)
    at Module._compile (internal/modules/cjs/loader.js:816:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:827:10)
    at Module.load (internal/modules/cjs/loader.js:685:32)
    at Function.Module._load (internal/modules/cjs/loader.js:620:12) type: 'Error', '$error': '$error' }

编译后发生错误。
错误前的最后一个构建触发器是:
https://github.com/zeit/next.js/blob/5a54e8715a7a7a92175addc19f4ec9f8f7bbd2e7/packages/next/build/index.ts#L342

再现

https://github.com/jpbow/module-register-build-issue

预期行为

next build成功

系统信息

  • 操作系统:Arch Linux
  • Next.js 版本:9.0.0

附加上下文

试过yarn install --force和 Clean 取决于安装。

next . dev-command 正常工作。

最有用的评论

我有一种感觉,它是由新添加的构建报告引起的。 因为构建完成后会抛出错误

Creating an optimized production build ...

> Using external babel configuration
> Location: "/app/.babelrc"
Compiled successfully.

Warning: You have opted-out of Automatic Prerendering due to `getInitialProps` in `pages/_app`.
Read more: https://err.sh/next.js/opt-out-automatic-prerendering

> Build error occurred
{ Error: Module did not self-register.
    at Object.Module._extensions..node (internal/modules/cjs/loader.js:730:18)
    at Module.load (internal/modules/cjs/loader.js:600:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
    at Function.Module._load (internal/modules/cjs/loader.js:531:3)
    at Module.require (internal/modules/cjs/loader.js:637:17)
    at require (internal/modules/cjs/helpers.js:22:18)
    at Object.<anonymous> (/app/node_modules/@firebase/firestore/node_modules/grpc/src/grpc_extension.js:32:13)
    at Module._compile (internal/modules/cjs/loader.js:701:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
    at Module.load (internal/modules/cjs/loader.js:600:32) type: 'Error', '$error': '$error' }
error Command failed with exit code 1.

所有55条评论

请提供完整的复制品

明天我会提供复制品。 今天我无法访问我的电脑。

关闭,因为它是 firebase grpc 问题。

#7821 也有类似问题

关闭,因为它是 firebase grpc 问题。

我该如何解决?
我有同样的问题

@go-rani 目前我没有解决方案,因为我不完全确定这个问题属于 firebase。 我试图在示例项目中重现这个错误。

我也有同样的问题。 降级到v8就正常了
这是我正在使用的图像:
gcr.io/google_appengine/nodejs

@vbilici有趣...

我有一种感觉,它是由新添加的构建报告引起的。 因为构建完成后会抛出错误

Creating an optimized production build ...

> Using external babel configuration
> Location: "/app/.babelrc"
Compiled successfully.

Warning: You have opted-out of Automatic Prerendering due to `getInitialProps` in `pages/_app`.
Read more: https://err.sh/next.js/opt-out-automatic-prerendering

> Build error occurred
{ Error: Module did not self-register.
    at Object.Module._extensions..node (internal/modules/cjs/loader.js:730:18)
    at Module.load (internal/modules/cjs/loader.js:600:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
    at Function.Module._load (internal/modules/cjs/loader.js:531:3)
    at Module.require (internal/modules/cjs/loader.js:637:17)
    at require (internal/modules/cjs/helpers.js:22:18)
    at Object.<anonymous> (/app/node_modules/@firebase/firestore/node_modules/grpc/src/grpc_extension.js:32:13)
    at Module._compile (internal/modules/cjs/loader.js:701:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
    at Module.load (internal/modules/cjs/loader.js:600:32) type: 'Error', '$error': '$error' }
error Command failed with exit code 1.

我有同样的问题。

但是当我注释掉“import firebase/firestore”时,这个问题并没有发生。

import firebase from 'firebase/app';
import 'firebase/auth';
import 'firebase/functions';
import 'firebase/database';
// import 'firebase/firestore'; <-- 

但我需要firebase/firestore。 这不是解决方案。

据我了解,grpc 破坏 nextjs 构建的任何依赖关系?

也遇到这个问题。

我一开始可以构建,但是当我的 /pages 变大时就会发生这种情况。 我尝试恢复到旧提交,在那里我可以毫无问题地进行构建,并在构建后复制我的 /pages 文件夹(-v1、-v2 等)构建到失败的程度。

我无法量化“大”有多大,但希望这会有所帮助。

@timneutkens @Vista1nik我在这里创建了一个复制品https://github.com/jpbow/module-register-build-issue

@jpbow感谢您的转载,已添加到问题中。

只是插一句,这在使用 now 2.0 部署时也会失败

NextJS 9 似乎与无论如何都使用 GRPC 的依赖项不兼容。
目前我试图找到任何其他使用 GRPC 来确认的包。

@Vista1nik它会

使用以下内容更新我的 next.config.js 后(如 #6073 中所述)

 webpack(config, options) {
    if (process.env.NODE_ENV !== "development") {
      config.externals = {
        grpc: "grpc"
      };
    }
    return config;
  }

链接: https :

我的部署错误发生了变化,我认为这不是最佳选择,但对于尝试更新生产版本的其他人来说可能是一个临时解决方法

新错误似乎与 AMP 工具箱有关,

running "yarn run now-build"
Jul 22 2019 14:05:48:345 | ...next.config.js | yarn run v1.16.0
Jul 22 2019 14:05:48:403 | ...next.config.js | $ next build
Jul 22 2019 14:05:49:159 | ...next.config.js | Creating an optimized production build ...
Jul 22 2019 14:05:49:824 | ...next.config.js | > Using external babel configuration
Jul 22 2019 14:05:49:824 | ...next.config.js | > Location: "/tmp/bc0fa1c/babel.config.js"
Jul 22 2019 14:07:52:235 | ...next.config.js | Failed to compile.
Jul 22 2019 14:07:52:235 | ...next.config.js | ./node_modules/amp-toolbox-optimizer/lib/transformers/AddBlurryImagePlaceholders.js
Jul 22 2019 14:07:52:235 | ...next.config.js | Module not found: Can't resolve 'jimp' in '/tmp/bc0fa1c/node_modules/amp-toolbox-optimizer/lib/transformers'
Jul 22 2019 14:07:52:235 | ...next.config.js |  

这很烦人,因为我根本没有使用 AMP,不确定这有多大关系,但希望它有帮助。

我在使用 firebase 时遇到了同样的问题.. 有没有人找到部署解决方法?

出于某种原因,我在 mac OS _works_ 上构建我的项目,而在 Debian 上,我得到了相同的“模块未自行注册”错误。

有没有人找到解决这个问题的方法?

一定有关于“购买新 Mac”的笑话。
目前只有降级是解决方案。

经过测试,#8290 修复了它。 包含它的金丝雀版本何时发布?

@getorca ,修复程序应该在v9.0.4-canary.3中,现在已经启动

此更改已发布为[email protected] 。 您可以升级您的应用程序!

似乎这在9.0.6退步9.0.59.0.4仍然可以正常构建。 使用9.0.6我得到关于grpc的相同错误

@ebemunk请提供一个清晰简洁的复制品,我们可以看看。 发布“在我的情况下不起作用”并不能解决您的问题。

感谢您查看@timneutkens ,抱歉我没有提供复制步骤,因为问题与已经报告的问题完全相同,并且之前的复制步骤仍然有效。
这是一个最小的例子,使用@jpbow创建的https://github.com/jpbow/module-register-build-issue

git clone https://github.com/jpbow/module-register-build-issue.git
cd module-register-build-issue
yarn add [email protected]
yarn build

你得到完全相同的错误重现我以前的评论:

Creating an optimized production build .Automatically opError: Failed to load /xxxxx/module-register-build-issue/node_modules/grpc/src/node/extension_binary/node-v72-darwin-x64-unknown/grpc_node.node. Module did not self-register.

为了了解它为什么是回归,我做了

yarn add [email protected]
yarn build

它按预期成功。 尝试修复此问题的版本:

yarn add [email protected]
yarn build

再次成功。 在此修复程序登陆之前尝试一个版本:

yarn add [email protected]
yarn build

失败并显示与上述相同的消息。

临界线似乎是import "firebase/firestore"; 。 如果删除此行,则不会观察到grpc错误。

希望有助于澄清。

在成功构建数十次后,我也突然收到该错误,但似乎更新到 9.0.6 修复了它。

9.1.1 和 firestore 仍然有这个问题。

编辑:降级到 9.0.5

9.1.1 和 firestore 仍然有这个问题。

降级到 9.0.4 或 9.0.5 修复它...

Error occurred prerendering /services: Error: Failed to load /Users/me/myproject/node_modules/grpc/src/node/extension_binary/node-v67-darwin-x64-unknown/grpc_node.node. Module did not self-register. at Object.Module._extensions..node (internal/modules/cjs/loader.js:775:18) at Module.load (internal/modules/cjs/loader.js:626:32) at tryModuleLoad (internal/modules/cjs/loader.js:566:12) at Function.Module._load (internal/modules/cjs/loader.js:558:3) at Module.require (internal/modules/cjs/loader.js:663:17) at require (internal/modules/cjs/helpers.js:20:18) at Object.<anonymous> (/Users/me/myproject/node_modules/grpc/src/grpc_extension.js:32:13) at Module._compile (internal/modules/cjs/loader.js:734:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:745:10) at Module.load (internal/modules/cjs/loader.js:626:32)

每个页面都发生此错误

fwiw 这不是真正的 Next.js 问题。 似乎grpc在特定的 Node.js 版本上中断。 前几天我在节点 12 上遇到了这个问题,它在节点 10 上运行良好。

@timneutkens
但是 Next 在 9.0.5 以上的版本中使用什么会产生崩溃?
我保持相同的 Node 版本(11.9.0),我只是切换下一个版本,当它高于 9.0.5 时就会出现问题。 我同意这个问题是由grpc引起的,但我们能否最终隔离 9.0.6 和 grpc 中的下一个更改之间的链接以找到解决方法?

但是 Next 在 9.0.5 以上的版本中使用什么会产生崩溃?

请随时调查导致问题的原因。

在 9.0.6-canary.0 上工作

在 9.0.6-canary.1 上崩溃

我的意思更像是调查,因为实际上要找出问题🚀

我做了一个git bisect并确定https://github.com/zeit/next.js/commit/f81b6d56e03d3f883e8c5faebae44208bd1a328b是第一个与 firebase/grpc 一起打破next build提交。

我认为这与https://github.com/grpc/grpc-node/issues/778有关

我很乐意帮助解决这个问题,但是我目前对正确的解决方案可能是什么样子有点迷茫。

任何想法@ijjk@Timer ,@timneutkens?

我测试了 9.0.6-canary.1 上的每个补丁,错误发生在: 61b8b73

我明天会继续调查,但实际上我注意到在 worker.js 的第 117 行
const components = await loadComponents( distDir, buildId, page, serverless )

失败然后

catch (error) { console.error( \n预渲染 ${path} 时发生错误: , error) return { ...results, error: true } } }

@notiles这很有趣。 我在v9.0.5v9.0.6-canary.1 git bisect之间做了一个v9.0.6-canary.1 ,发现第一个错误提交是https://github.com/zeit/next.js/commit/f81b6d56e03d3f883e8c5faebae44208bd1a328b。

虽然您提到的提交(https://github.com/zeit/next.js/commit/61b8b7323e089c8cc0372ce0f32402c8d70096f8)对我来说也绝对是坏的,但在那之前的提交(https://github.com/zeit/next .js/commit/110c83fa443a7a557a82dc49968dee56e293680c)对我来说也坏了。

您可以使用https://github.com/zeit/next.js/commit/f81b6d56e03d3f883e8c5faebae44208bd1a328b尝试一下,看看它是否仍然适合您?

@ctavan早上好! 在我这边f81b6d5正在工作,我的构建成功了。

我找到它崩溃的地方,但我不能更进一步:

在包/下一个/下一个服务器/服务器/负载组件.ts
第 70 行:interopDefault(require(appPath)) 失败。

appPath 看起来不错:/Users/me/myproject/.next/server/static/eUfibox1CSt3iNdo6twQi/pages/_app

@notiles这真的,真的很奇怪! 您使用的是什么节点版本和操作系统?

以下是重现如何在我的机器上可靠重现问题的详细步骤。 你也想尝试一下,让我知道它是否真的适合你https://github.com/zeit/next.js/commit/f81b6d56e03d3f883e8c5faebae44208bd1a328b吗?

# MacOS 10.14.6 (18G103)
#
node --version
# v12.12.0

# Steps to reproduce:
git clone https://github.com/zeit/next.js.git
cd next.js

# Check out the last working commit:
git checkout 2c7b4d8a

# Build local next module:
yarn

# Demonstrate the issue using the included example:
cd examples/with-firebase-authentication

# Use local next module:
sed -i '' -e "s#\"next\": \"latest\"#\"next\": \"file:$(pwd)/../../packages/next\"#" package.json

# Upgrade firebase modules because the ones defined in package.json won't build
# with node 12:
yarn add [email protected] [email protected]

# Duplicate the single page from the example to provoke a parallel build. The
# issue only appears when multiple pages are built in parallel:
cp pages/index.js pages/parallel.js

# Ensure there's no local installation of next, then force install the example
# and try to build it:
rm -rf node_modules/next && yarn install --force && yarn build

# yarn run v1.19.1
# warning package.json: No license field
# $ next build
# Creating an optimized production build ...
#
# Compiled successfully.
#
# Page            Size     Files  Packages
# ┌ σ /           3.23 kB      1         7
# ├   /_app       1.83 kB      0         7
# ├   /_document
# ├   /_error     7.71 kB      0         7
# └ σ /parallel   3.24 kB      1         7
#
# σ  (Server)       page will be server rendered (i.e. getInitialProps)
# ⚡  (Static File)  page was prerendered as static HTML
#
# ✨  Done in 19.53s.

# Now back to the repository root and checkout the first broken commit:
cd ../../
git checkout f81b6d5

# Build the local next module again:
yarn

# Try to build the example again:
cd examples/with-firebase-authentication
rm -rf node_modules/next && yarn install --force && yarn build

# yarn run v1.19.1
# warning package.json: No license field
# $ next build
# Creating an optimized production build ...
#
# Compiled successfully.
#
# > Build error occurred
# Error: Failed to load /Users/*REDACTED*/next.js/examples/with-firebase-authentication/node_modules/grpc/src/node/extension_binary/node-v72-darwin-x64-unknown/grpc_node.node. Module did not self-register.
#     at Object.Module._extensions..node (internal/modules/cjs/loader.js:1003:18)
#     at Module.load (internal/modules/cjs/loader.js:812:32)
#     at Function.Module._load (internal/modules/cjs/loader.js:724:14)
#     at Module.require (internal/modules/cjs/loader.js:849:19)
#     at require (internal/modules/cjs/helpers.js:74:18)
#     at Object.<anonymous> (/Users/*REDACTED*/next.js/examples/with-firebase-authentication/node_modules/grpc/src/grpc_extension.js:32:13)
#     at Module._compile (internal/modules/cjs/loader.js:956:30)
#     at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)
#     at Module.load (internal/modules/cjs/loader.js:812:32)
#     at Function.Module._load (internal/modules/cjs/loader.js:724:14) {
#   type: 'Error'
# }
# error Command failed with exit code 1.
# info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

我也在 Mac OS 10.14.6 但我在节点 11.9.0 我会试着告诉你我是否可以构建

像你一样,它会与 f81b6d5 崩溃。

在我的项目中我使用

"react-redux-firebase": "^2.4.1",
"redux-firestore": "^0.9.0",
"firebase": "^7.1.0",

我将我的 firebase 更新到 7.2.1 并尝试使用 61b8b73 并成功构建......真的很奇怪

尝试删除此导入:导入“firebase/firestore”; 仅在 index.js 中

@notiles如果您删除两个firebase/firestore导入中的一个,那么您将不再引发需要grpc的两个模块的并行构建。

如上所述,我非常有信心我们在这里观察到的问题是由https://github.com/grpc/grpc-node/issues/778引起的,它指出grpc “不支持多线程”。

作为一种解决方法,我发现通过以下next.config.js构建成功:

module.exports = {
  experimental: {
    cpus: 1,
  },
};

并且一旦增加 CPU 数量就会失败(这进一步证明并行工作线程执行是根本原因)。 显然,构建将不再利用多个工作线程,因此这是一种解决方法,而不是解决方案。

@ijjk@Timer@timneutkens很抱歉再次

很明显@ctavan ! 非常感谢这个解释。 不幸的是,我仍然有实验问题:{
处理器:1,
}

`警告:您已启用实验性功能。semver 未涵盖实验性功能,并且可能会导致意外或损坏的应用程序行为。

使用外部 babel 配置
位置:“/Users/me/myproject/.babelrc”
创建优化的生产构建

编译成功。

自动优化页面..
预渲染 /download/[document_id] 时发生错误:错误:无法加载 /Users/me/myproject/node_modules/grpc/src/node/extension_binary/node-v72-darwin-x64-unknown/grpc_node.node。 模块没有自注册。

等等...`

我从节点 11.9.0 切换到节点 10.12,从节点 13.0.1 切换到节点 12.13.0 没有成功......

我有点太快了。 使用我之前评论中的next.config.js构建确实适用于https://github.com/zeit/next.js/commit/f81b6d56e03d3f883e8c5faebae44208bd1a328b但不适用于[email protected]因为https://github。 com/zeit/next.js/commit/61b8b7323e089c8cc0372ce0f32402c8d70096f8jest-worker引入到next export ,作为构建步骤的一部分被调用。

我最初的想法是将config.experimental.cpus传递给出口工人,但是我没有成功。 即使设置了numWorkers: 1 ,构建中的导出步骤也会因Module did not self-register.错误而失败。

让它适用于我的用例的唯一方法是将enableWorkerThreads: false设置

https://github.com/zeit/next.js/blob/a964dc825ecea1e35fb460184828974e00b7d6d6/packages/next/export/index.ts#L265 -L273

一种可能的解决方法是允许设置config.experimental.cpus: 0并因此完全禁用工作线程。 我将很快为此建议提供拉取请求。

好的@ctavan ! 完全正确,我将 enableWorkerThreads 更改为 false 并在下一个 9.1.1 中构建成功

所以最后它更像是一个 grpc 问题而不是下一个问题? 你对此有什么感觉? 你认为我们必须在 grpc 上打开一个问题吗?

非常感谢您的所有解释,这真的很有帮助。

我现在提出了https://github.com/zeit/next.js/pull/9199 ,其中设置config.experimental.cpus: 1将完全禁用工作线程。 这使得next build在所有涉及grpc并且我已经测试过的用例中成功。

对我来说,这将是一个可以接受的解决方法,直到问题在grpcfirebasegrpc库(https://github.com/grpc/grpc-node/issues/778)中已经存在问题,但显然解决方案取决于节点(https://github. com/nodejs/nan/issues/844)。

我现在相信这是需要在grpc修复的东西,而不是在这里。 至少我没有看到在 Next.js 中修复它的直接方法而不回到旧的并行化技术,这肯定会损害代码库。

如果https://github.com/zeit/next.js/pull/9199被接受,我们至少会有一种方法来构建依赖于firebase/firestore ,即使构建时间可能会更长,因为我们不能利用并行性。

大家好!
只需添加

“引擎”: {
“节点”:“10.x”
}

到你的 package.json

欢迎您成为编码员。

对于正在跟踪此问题的任何其他人:

我可以确认在使用 grpc 时我不再收到“模块未自行注册”的消息。
v9.3.2 包含 #11347,这是修复它的原因

我有同样的错误

回购
建筑错误

Error: Error occurred while parsing your function triggers.

Error: 
Something went wrong installing the "sharp" module

Module did not self-register.
此页面是否有帮助?
0 / 5 - 0 等级

相关问题

pie6k picture pie6k  ·  3评论

knipferrc picture knipferrc  ·  3评论

renatorib picture renatorib  ·  3评论

havefive picture havefive  ·  3评论

YarivGilad picture YarivGilad  ·  3评论