deor

fix: Error [ERR_REQUIRE_ESM]: require() of ES Module string-width

This started happening with a typescript project after updating typescript version and ts-node version:


yarn start
yarn run v1.22.18
warning package.json: No license field
$ yarn migrate up && node dist/src/index.js
warning package.json: No license field
$ node ./node_modules/node-pg-migrate/bin/node-pg-migrate -d POSTGRES_URL -m ./src/migrations --no-check-order up
/project/node_modules/cliui/build/index.cjs:291
const stringWidth = require('string-width');
                    ^

Error [ERR_REQUIRE_ESM]: require() of ES Module /project/node_modules/string-width/index.js from /project/node_modules/cliui/build/index.cjs not supported.
Instead change the require of index.js in /project/node_modules/cliui/build/index.cjs to a dynamic import() which is available in all CommonJS modules.
    at Object.<anonymous> (/project/node_modules/cliui/build/index.cjs:291:21)
    at Object.<anonymous> (/project/node_modules/yargs/build/index.cjs:1:58136)
    at Object.<anonymous> (/project/node_modules/yargs/index.cjs:5:30)
    at Object.<anonymous> (/project/node_modules/node-pg-migrate/bin/node-pg-migrate:9:15) {
  code: 'ERR_REQUIRE_ESM'
}

Node.js v18.18.0
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Fix: Delete amy mention of string-width from yarn.lock and run yarn again. Or, if you do not care about your yarn.lock, delete the file and run yarn again.