kristiandupont / kanel

Generate Typescript types from Postgres
https://kristiandupont.github.io/kanel/
MIT License
829 stars 57 forks source link

code: 'ERR_REQUIRE_ESM' #545

Open astralarya opened 3 months ago

astralarya commented 3 months ago

Version: "kanel": "^3.8.8", node: v20.8.0

Attempting to run kanel. Config file below:

yarn run v1.22.19
$ kanel
/Users/marakim/Documents/code/github/astralarya/rrobin/web/next/node_modules/cli-progress/lib/formatter.js:1
const _stringWidth = require('string-width');
                     ^

Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/marakim/Documents/code/github/astralarya/rrobin/web/next/node_modules/string-width/index.js from /Users/marakim/Documents/code/github/astralarya/rrobin/web/next/node_modules/cli-progress/lib/formatter.js not supported.
Instead change the require of index.js in /Users/marakim/Documents/code/github/astralarya/rrobin/web/next/node_modules/cli-progress/lib/formatter.js to a dynamic import() which is available in all CommonJS modules.
    at Object.<anonymous> (/Users/marakim/Documents/code/github/astralarya/rrobin/web/next/node_modules/cli-progress/lib/formatter.js:1:22)
    at Object.<anonymous> (/Users/marakim/Documents/code/github/astralarya/rrobin/web/next/node_modules/cli-progress/lib/generic-bar.js:3:20)
    at Object.<anonymous> (/Users/marakim/Documents/code/github/astralarya/rrobin/web/next/node_modules/cli-progress/lib/single-bar.js:1:21)
    at Object.<anonymous> (/Users/marakim/Documents/code/github/astralarya/rrobin/web/next/node_modules/cli-progress/cli-progress.js:1:20)
    at Object.<anonymous> (/Users/marakim/Documents/code/github/astralarya/rrobin/web/next/node_modules/kanel/build/cli/main.js:8:40)
    at Object.<anonymous> (/Users/marakim/Documents/code/github/astralarya/rrobin/web/next/node_modules/kanel/build/cli/index.js:3:16)
    at Object.<anonymous> (/Users/marakim/Documents/code/github/astralarya/rrobin/web/next/node_modules/kanel/bin/kanel:4:1) {
  code: 'ERR_REQUIRE_ESM'

Content of .kanelrc.js:

/** @type {import('kanel').Config} */
module.exports = {
  connection: {
    host: process.env.PGHOST,
    user: process.env.PGUSER,
    password: process.env.PGPASSWORD,
    database: process.env.PGDATABASE,
  },

  preDeleteOutputFolder: true,
  outputPath: "./src/sql",
};
astralarya commented 3 months ago

This looks like it could be related: https://stackoverflow.com/questions/69081410/error-err-require-esm-require-of-es-module-not-supported

Maybe it would be fixed if you added:

"module": true,

to the package.json?

kristiandupont commented 3 months ago

Yeah, the issue is that Kanel is not (yet) ESM-ready. Sadly, it's not quite as easy as changing that one line, and making the change will affect everyone who is not setting "module": true in their package.json. I do intend to do something about this because I am facing issues with it myself, but I can't guarantee when.

If you want a hacky workaround, I know one friend who comments out the "module": true line in his package.json every time he runs Kanel and then undoes that afterwards. I know, not pretty, but it does work...

kevlened commented 3 months ago

I ran into this issue for a project that uses "type": "module".

My workaround was to rename .kanelrc.js to .kanelrc.cjs, then override the config import:

kanel --config .kanelrc.cjs

Edit: this isn't necessary, because kanel automatically recognizes .kanelrc.cjs