Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: require() of ES Module when trying to run a node container

Tags:

node.js

docker

I am trying to build my angular app using a Github Action. I got some errors in the action that are caused by the container used. Now I build a basic local container trying to run the linter as a first step.

Dockerfile

FROM node:18.18

COPY ./ ./

RUN yarn install
RUN yarn lint

If I try to build the container, I run into the following error:

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'
}

Some dependency is using require() instead of import, so the build of the container fails. Does anyone have a fix for that? I tried different node versions and images like bitnami/node, but the issue still exists. Somehow the error also only occurs when running a container. From my local node version the lint, test, build steps, all work fine.

like image 629
KastenBrot Avatar asked Jan 31 '26 20:01

KastenBrot


1 Answers

If someone in the future should have the same problem: There is a known issue with that dependency, that hasn't been updated yet. The workaround is removing the yarn.lock from the repo. See: github.com/iarna/wide-align/issues/63

Update:

As stated by multiple people in the issue, adding this to your package.json should also resolve the issue:

"resolutions": {
    "string-width": "4.2.3"
 }

Resolutions is a yarn feature, for npm use

 "overrides": {
     "string-width": "4.2.3"
 }
like image 121
KastenBrot Avatar answered Feb 03 '26 10:02

KastenBrot



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!