How to ship in CJS format when my package has pure ESM package dependencies?
See original GitHub issueindex.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:
- Created a year ago
- Comments:8 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thank You Kindly!
rollup.inlineDependencies
should not be what I want, I am currently using the5.x
version ofora
.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.