错误:尝试运行节点容器时 ES 模块的 require()

Kas*_*rot 3 node.js docker

我正在尝试使用 Github Action 构建我的角度应用程序。我在操作中遇到了一些由所使用的容器引起的错误。现在,我构建一个基本的本地容器,尝试运行 linter 作为第一步。

Dockerfile

FROM node:18.18

COPY ./ ./

RUN yarn install
RUN yarn lint
Run Code Online (Sandbox Code Playgroud)

如果我尝试构建容器,则会遇到以下错误:

Node.js v18.18.0"
info This module is OPTIONAL, you can safely ignore this error
error /node_modules/@parcel/watcher: Command failed.
Exit code: 1
Command: node-gyp-build
Arguments:
Directory: /node_modules/@parcel/watcher
Output:
/node_modules/wide-align/align.js:2
var stringWidth = require('string-width')
                  ^

Error [ERR_REQUIRE_ESM]: require() of ES Module /node_modules/string-width/index.js from /node_modules/wide-align/align.js not supported.
Instead change the require of index.js in /node_modules/wide-align/align.js to a dynamic import() which is available in all CommonJS modules.
    at Object.<anonymous> (/node_modules/wide-align/align.js:2:19)
    at Object.<anonymous> (/node_modules/gauge/lib/render-template.js:2:13)
    at Object.<anonymous> (/node_modules/gauge/lib/plumbing.js:3:22)
    at Object.<anonymous> (/node_modules/gauge/lib/index.js:2:16)
    at Object.<anonymous> (/node_modules/npmlog/lib/log.js:3:13)
    at Object.<anonymous> (/node_modules/node-gyp/lib/node-gyp.js:5:13)
    at Object.<anonymous> (/node_modules/node-gyp/bin/node-gyp.js:8:13) {
  code: 'ERR_REQUIRE_ESM'
}
Run Code Online (Sandbox Code Playgroud)

某些依赖项正在使用require()而不是import,因此容器的构建失败。有人能解决这个问题吗?我尝试了不同的节点版本和图像,例如 bitnami/node,但问题仍然存在。不知何故,该错误也仅在运行容器时发生。从我的本地节点版本来看,lint、测试、构建步骤都工作正常。

Kas*_*rot 9

如果将来有人遇到同样的问题:该依赖项存在一个已知问题,但尚未更新。解决方法是从存储库中删除yarn.lock。请参阅:github.com/iarna/wide-align/issues/63

更新:

正如该问题中多人所述,将其添加到 package.json 也应该可以解决该问题:

"resolutions": {
    "string-width": "4.2.3"
 }
Run Code Online (Sandbox Code Playgroud)

分辨率是一个纱线功能,供 npm 使用

 "overrides": {
     "string-width": "4.2.3"
 }
Run Code Online (Sandbox Code Playgroud)