thoughtbot / belt

Belt is a CLI for starting a new React Native Expo app and will even keep your pants secure as you continue development.
MIT License
131 stars 3 forks source link

[Experiment] Change output to CJS instead of ESM #29

Closed stevehanson closed 9 months ago

stevehanson commented 10 months ago

Was getting an error in existing Expo apps:

Perform project setup and redundant tasks
    without your pants falling down!

/Users/shanson/dev/thoughtbelt/node_modules/string-width/index.js:2
const stripAnsi = require('strip-ansi');
                  ^

Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/shanson/dev/thoughtbelt/node_modules/strip-ansi/index.js from /Users/shanson/dev/thoughtbelt/node_modules/string-width/index.js not supported.
Instead change the require of
/Users/shanson/dev/thoughtbelt/node_modules/strip-ansi/index.js in
/Users/shanson/dev/thoughtbelt/node_modules/string-width/index.js to a
dynamic import() which is available in all CommonJS modules.
    at Object.<anonymous> (/Users/shanson/dev/thoughtbelt/node_modules/string-width/index.js:2:19) {
  code: 'ERR_REQUIRE_ESM'
}

Node.js v21.1.0
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about
this command.
stevehanson commented 10 months ago

I tried this out, but then some of our dependencies, like Chalk, don’t work since they are pure ESM, which doesn’t work together with CJS. I deployed a beta thoughtbelt version 0.1.10 (BETA) with that change, but when I run it, I get this error:

/Users/shanson/dev/thoughtbelt/dist/index.cjs:919
var import_chalk = __toESM(require("chalk"), 1);
                           ^

Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/shanson/dev/thoughtbelt/node_modules/chalk/source/index.js from /Users/shanson/dev/thoughtbelt/dist/index.cjs not supported.
Instead change the require of index.js in /Users/shanson/dev/thoughtbelt/dist/index.cjs to a dynamic import() which is available in all CommonJS modules.
    at Object.<anonymous> (/Users/shanson/dev/thoughtbelt/dist/index.cjs:919:28) {
  code: 'ERR_REQUIRE_ESM'
}

I don't have more time right now to dig into this. I’m guessing we never ran a thoughtbelt script in an existing project after setting up tsup compilation.