sindresorhus / ora

Elegant terminal spinner
MIT License
9.14k stars 270 forks source link

Using with typescript #197

Closed Rxann closed 2 years ago

Rxann commented 2 years ago

When I try to use ora with typescript, I get an error when running the compiled file with node.

The error:

   const ora_1 = __importDefault(require("ora"));
                              ^

Error [ERR_REQUIRE_ESM]: require() of ES Module D:\GitHub\license-generator\node_modules\ora\index.js from D:\GitHub\license-generator\src\dist\test.js not supported.
Instead change the require of index.js in D:\GitHub\license-generator\src\dist\test.js to a dynamic import() which is available in all CommonJS modules.
    at Object.<anonymous> (D:\GitHub\license-generator\src\dist\test.js:8:31) {
  code: 'ERR_REQUIRE_ESM'
}

Is this a my code issue or an ESM issue?

jason-henriksen commented 2 years ago

This is not an ora problem, but it is a huge hassle to find a correct example of the typescript code. I have an example working, but I'm in transit today. I'm hoping to post it to GitHub tonight and will comment here once it is available.

Ora is a great project. I'm looking forward to getting the sample up so it can be used more easily.

Rxann commented 2 years ago

Thank you so much and whenever you can upload the example that would be great!

jason-henriksen commented 2 years ago

Take a look at this boilerplate:
For me, it successfully gets TS and Ora play nice together.

https://github.com/jason-henriksen/typescript-with-esm-no-babel-boilerplate.git

Rxann commented 2 years ago

Thank you for this!

I am just wondering if you know of any other alternatives to this?

sindresorhus commented 2 years ago

This is not the place to document how to use ESM with TypeScript. It has nothing specifically to do with Ora.

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

jason-henriksen commented 2 years ago

... because roughly 10 people asking "how can I use Ora" clearly has nothing to do with Ora...?

The items under your "Related" section in your readme have nothing specifically to do with Ora either.
But whatever. Your project, your call.

@Rxann , I was only looking for a TypeScript-no-babel way to do a native ESM import. You can message me directly if you have some other specific question about that boiler plate, or make an issue on the boiler plate and I'll see what I can do to help.

Rxann commented 2 years ago

Ok sounds good!

nephix commented 2 years ago

My workaround:

$ npm install ora@^3.2.0
$ npm install @types/ora --save-dev

May miss some features but the API is still pretty simple.

My 2cents on:

It has nothing specifically to do with Ora

Our codebase already uses ES modules, even in the same script. ora@6.0.0 is the only one causing trouble. Maybe adding a GitHub action step which compiles a typescript sample file can help.

Rxann commented 2 years ago

Ok thank you!