sindresorhus / ora

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

Ora in TypeScript: Import Problem #195

Closed JasonHenriksen closed 2 years ago

JasonHenriksen commented 2 years ago

I'm trying to use ora in a typescript project, but no matter what I do, I get this error when I'm trying to import ora in jest ( so that I can mock it. ):

SyntaxError: Cannot use import statement outside a module

I'm using the boilerplate typescript here: https://github.com/metachris/typescript-boilerplate

I'm doing the import {oraPromise} from 'ora'

command once in a main .ts file and again in a mail.test.ts file. Any idea how I can make ora import correctly? Or an example typescript project that uses ora?

Thank you!

sindresorhus commented 2 years ago

Ora is just an ESM package. There's nothing special about it. I would recommend googling "ESM TypeScript".

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

JasonHenriksen commented 2 years ago

Well, that was deeply unsatisfying, but I can agree that the problem is in the ecosystem, not in your specific code. I've spent almost three days trying to get this to work, but I can at least get the spinner to work even though jest and eslint are still failing. When (if?) I get something that actually completely works, I'll post a link to a repo here. But "out of the box" the ESM module that is ora is not easily compatible with typescript boiler plate projects. Hopefully next comment coming soon with a repo link.

jason-henriksen commented 2 years ago

In the end, this is how I got it to work:

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

jason-henriksen commented 2 years ago

BTW, I tried to PR this in, but of course I lack permissions. If you'd like, you're welcome to add this to your readme.

`## TypeScript Users

A couple of TypeScript users got really confused about ESM and CommonJS. Even though none of that is Ora's fault, it added unnecessary issues that had to be closed. If you're using TypeScript and you'd like a minimalist example of using Ora from a TypeScript/Node CLI. Try looking here: https://github.com/jason-henriksen/typescript-with-esm-no-babel-boilerplate`

ctsstc commented 1 year ago

This has become such a time sink nightmare that I have to look elsewhere. I've spent hours and cannot spend days on a spinner. I know this isn't your fault and it's the whole jest + node + TS + ESM ecosystem. I've been down this road for other things always to abandon it. I hope someday this issue ceases to exist.