question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

index.ts:

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

build.config.ts:

export default defineBuildConfig({
  entries: [
    'index',
  ],
  rollup: {
    emitCJS: true,
  },
})

run unbuild && node dist/index.cjs and get error:

const ora = require('ora');
            ^

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

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
holazzcommented, Jun 3, 2022

Anthony Fu helped me, I just needed to make execa a development dependency instead of a normal one, which completely makes sense!

So @holazz to solve the issue, use rollup.inlineDependencies and make your ESM-only dependency a development dependency.

Alternatively, as @pi0 said, find the same lib exported as CJS (ora v5.x), or finally, use dynamic imports.

Hopefully this solves the issue for anyone stumbling upon it.

Thank You Kindly! rollup.inlineDependencies should not be what I want, I am currently using the 5.x version of ora.

1reaction
innocenzicommented, Jun 3, 2022

Anthony Fu helped me, I just needed to make execa a development dependency instead of a normal one, which completely makes sense!

So @holazz to solve the issue, use rollup.inlineDependencies and make your ESM-only dependency a development dependency.

Alternatively, as @pi0 said, find the same lib exported as CJS (ora v5.x), or finally, use dynamic imports.

Hopefully this solves the issue for anyone stumbling upon it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Publish ESM and CJS in a single package - Anthony Fu
A short tutorial of shipping both ESM and CJS dual formats in a single NPM package.
Read more >
Pure ESM package - gists · GitHub
Pure ESM package. The package that linked you here is now pure ESM. ... Bundle your dependencies with Webpack into a CommonJS bundle....
Read more >
How to Create a Hybrid NPM Module for ESM and CommonJS.
The problem is what happens when you need a package to be a hybrid and export both ESM and CommonJS formats? Unfortunately there...
Read more >
Migrating an NPM package to use ES Modules - Medium
... to convert an NPM package from using CommonJS (CJS) modules, to the newer ES module (ESM) format. ... Introducing a new module-format...
Read more >
What does it take to support Node.js ESM? – The Guild
Explore our services and get in touch. ECMAScript modules, also known as ESM, is the official standard format to package JavaScript, and ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found