How to fix "require is not defined"? #1321
-
Beta Was this translation helpful? Give feedback.
Replies: 9 comments 14 replies
-
Usually this happens when a package tells us that it's ESM but it then still contains CJS A quick look at jotai's ESM code seems fine (https://unpkg.com/browse/[email protected]/index.js) but then it imports use-context-selector, which does mix ESM & CJS: https://unpkg.com/browse/[email protected]/dist/index.modern.js We can add a workaround for this specific package, but only if the package author is unresponsive. Can you file an issue with that repo for them to solve on their end? All Rollup projects would be affected by this issue and unable to use this package, so it's probably a bigger deal then they realize |
Beta Was this translation helpful? Give feedback.
-
I'm having a similar issue with the package It's export is a single
Error:
If I had to guess, either the complete lack of imports or the use of the relatively uncommon |
Beta Was this translation helpful? Give feedback.
-
Not to completely hijack this thread, but I'm getting the same problem when having just plopped in my old CRA app into a snowpack app. Running this code gives me the
I'm assuming there's some sort of babel plugin that I'm missing here? |
Beta Was this translation helpful? Give feedback.
-
Is there a way to tell snowpack, not to trust a package so it takes the needed steps to fix this? |
Beta Was this translation helpful? Give feedback.
-
I was seeing this error when importing a grpc-web generated file with the commonjs option using typescript. The generated file uses |
Beta Was this translation helpful? Give feedback.
-
I use rollup-plugin-re to transform
|
Beta Was this translation helpful? Give feedback.
-
a workaround: in index.ts if (import.meta.env.MODE === 'production') {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
window.require = function(id: string): object {
switch (id) {
case './local/zh_CN':
return {
confirmLabel: 'OK',
backspaceLabel: 'backspace',
cancelKeyboardLabel: 'cancel keyboard'
}
default:
return {}
}
}
} |
Beta Was this translation helpful? Give feedback.
-
I also encountered the same problem,seek solution~~~!!!!!!! |
Beta Was this translation helpful? Give feedback.
-
This works for me - make sure to delete const commonjs = require('@rollup/plugin-commonjs')
// ...
packageOptions: {
polyfillNode: true,
// convert mixed 'liar' packages from commonjs
rollup: {
plugins: [
commonjs({
include: [
// packages
'node_modules/filestack-react/**/*.js',
'node_modules/filestack-js/**/*.js'
],
// the mixing is the problem
transformMixedEsModules: true
})
]
}
}, |
Beta Was this translation helpful? Give feedback.
Usually this happens when a package tells us that it's ESM but it then still contains CJS
require()
calls. If a package tells us it's ESM we'll trust it, but in some cases that's not true.A quick look at jotai's ESM code seems fine (https://unpkg.com/browse/[email protected]/index.js) but then it imports use-context-selector, which does mix ESM & CJS: https://unpkg.com/browse/[email protected]/dist/index.modern.js
We can add a workaround for this specific package, but only if the package author is unresponsive. Can you file an issue with that repo for them to solve on their end? All Rollup projects would be affected by this issue and unable to use this package, so it's probably a bigg…