Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to ship in CJS format when my package has pure ESM package dependencies? #628

Open
holazz opened this issue May 16, 2022 · 4 comments
Open

Comments

@holazz
Copy link

holazz commented May 16, 2022

index.ts:

import ora from 'ora'

const spinner = ora('Starting...').start()
spinner.succeed('Done!')

run tsup index.ts --format cjs,esm && node dist/index.js and get error:

var import_ora = __toESM(require("ora"));
                         ^

Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/zhangjiawei/Desktop/github-downloader/node_modules/.pnpm/ora@6.1.0/node_modules/ora/index.js from /Users/zhangjiawei/Desktop/github-downloader/dist/index.js not supported.
Instead change the require of /Users/zhangjiawei/Desktop/github-downloader/node_modules/.pnpm/ora@6.1.0/node_modules/ora/index.js in /Users/zhangjiawei/Desktop/github-downloader/dist/index.js to a dynamic import() which is available in all CommonJS modules.
    at Object.<anonymous> (/Users/zhangjiawei/Desktop/github-downloader/dist/index.js:47:26) {
  code: 'ERR_REQUIRE_ESM'
}

Upvote & Fund

  • We're using Polar.sh so you can upvote and help fund this issue.
  • We receive the funding once the issue is completed & confirmed by you.
  • Thank you in advance for helping prioritize & fund our backlog.
Fund with Polar
@9oelM
Copy link
Contributor

9oelM commented Jun 14, 2022

Well I had to wrap it around my head for this problem, and the solution was not using CJS format but just go with ESM. It takes too much effort to convert all child node_modules into CJS if they are indeed ESM because I gotta use some extra tools like babel or whatnot. Tried all of that and it didn't work.

The annoying ERR_REQUIRE_ESM error disappears when you do:

tsup index.ts --format esm 

npm i -g ts-node

node --loader=ts-node/esm dist/index.mjs

Note that you are using index.mjs, not index.js.

@egoist
Copy link
Owner

egoist commented Jun 14, 2022

@9oelM that's one way to do it, you can also just bundle the pure esm packages if you want.

@OmgImAlexis
Copy link

All you need todo is install ora as a dev dep and you're good to go.

Any time you see a dep that's ESM simply use npm i -D instead of npm i and then tsup will bundle it and everything will be good.

@holazz
Copy link
Author

holazz commented Jun 20, 2022

Thanks, I wish to ship both cjs and esm format, I have tried all these ways, and finally, I switched to the 5.x version of ora.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants