sindresorhus/ora

Node / TypeScript / ESM Issues

EthanRBrown opened this issue · 2 comments

I've used Ora in the past and liked it, so I didn't think twice about throwing it in a TS project I'm working on. An hour of frustration later, I'm giving up on it. I did try a minimum reproduction of the issue, which I'll describe here (I tried various versions back to 5.0.0 with the same results, so this isn't a recent issue).

$ npm i -D typescript ts-node @types/node
$ npm i ora

Create test.ts:

import ora from 'ora'
const spinner = ora('Doing stuff').start()
setTimeout(() => spinner.succeed('Done!'), 1000)

Attempt to run with ts-node (skipping type-checking...not the problem here):

$ ./node_modules/.bin/ts-node -T test.ts
/tmp/ora-test/node_modules/ts-node/dist/index.js:851
            return old(m, filename);
                   ^
Error [ERR_REQUIRE_ESM]: require() of ES Module /tmp/ora-test/node_modules/ora/index.js from /tmp/ora-test/test.ts not supported.
Instead change the require of index.js in /tmp/ora-test/test.ts to a dynamic import() which is available in all CommonJS modules.
    at require.extensions.<computed> [as .js] (/tmp/ora-test/node_modules/ts-node/dist/index.js:851:20)
    at Object.<anonymous> (/tmp/ora-test/test.ts:3:13)
    at m._compile (/tmp/ora-test/node_modules/ts-node/dist/index.js:857:29)
    at require.extensions.<computed> [as .ts] (/tmp/ora-test/node_modules/ts-node/dist/index.js:859:16) {
  code: 'ERR_REQUIRE_ESM'
}

I also tried with a couple of "real" TypeScript configurations (i.e. not using ts-node), and experimented with the module type, and ESM flags, and nothing worked.

I could tackle a PR myself, but I won't have time for a couple of weeks.

The problem is with your TS or ts-node config, not this package.

https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c

Thanks, @sindresorhus. That guide is very helpful, and answers some questions I've had about CJS->ESM conversion.

For anyone finding this, the problem was really ts-node more than anything else: I switched to npx and had no problems whatsoever.