vite: vite does not seem to support es module that depends on nodejs builtins
Describe the bug
By default vite does not include any support for es module that import nodejs builtins module (like crypto, etc…)
I tried to add the rollup-plugin-node-builtins
plugin with option {crypto: true}
but I had to set a name to it as the following
import builtins from 'rollup-plugin-node-builtins';
const builtinsPlugin = builtins({crypto: true});
builtinsPlugin.name = 'builtins';
module.exports = {
rollupInputOptions: {
plugins: [
builtinsPlugin
]
}
}
because otherwise I get the following error :
TypeError: Cannot read property 'includes' of undefined
at C:\dev\projects\wighawag\svite-test\svite-typescript-minimal\node_modules\vite\dist\node\build\index.js:202:60
at Array.findIndex (<anonymous>)
at Object.build (C:\dev\projects\wighawag\svite-test\svite-typescript-minimal\node_modules\vite\dist\node\build\index.js:202:36)
at async runBuild (C:\dev\projects\wighawag\svite-test\svite-typescript-minimal\node_modules\svite\bin\svite.js:176:7)
at async Command.<anonymous> (C:\dev\projects\wighawag\svite-test\svite-typescript-minimal\node_modules\svite\bin\svite.js:401:7)
at async Promise.all (index 0)
at async main (C:\dev\projects\wighawag\svite-test\svite-typescript-minimal\node_modules\svite\bin\svite.js:441:3)
Also when running npm run dev
it fails with various error indicating the builtins are not properly injected
Reproduction
Here is repo that uses svite
(https://github.com/dominikg/svite/) where you can reproduce the issue in the branch rollup-plugin-node-builtins
: https://github.com/wighawag/svite-typescript-minimal/tree/rollup-plugin-node-builtins
npm i
npm run build
to test runtime error :
npm run dev
System Info
- required
vite
version: 1.0.0-rc.4 - required Operating System: Windows 10 (64bit)
- required Node version: 12.18.3
- npm/yarn version : npm 6.14.6
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 31 (11 by maintainers)
Commits related to this issue
- fix: add `@rollup/plugin-commonjs` before user plugin, let it correctly output es module fix #728 — committed to rolldown/vite by underfin 4 years ago
- fix: add `@rollup/plugin-commonjs` before user plugin, let it correct… (#746) fix #728 — committed to vitejs/vite by underfin 4 years ago
That’s a problem in
rollup-plugin-node-globals
. It’s very outdated and is probably using an old version of acorn to parse the code in script mode instead of module mode.Suggestion: if you have dependencies that expects to use Node built-ins, try swapping it out with something more modern. Relying on Node built-ins in the browser is bad practice, period.
You can’t use
require
in your source code. Vite is ESM only.Leaving this clue here for anyone who’s stumbling into similar problems. https://github.com/snowpackjs/rollup-plugin-polyfill-node
可以如何配置兼容吗?项目中引入第三方库包含require
@wighawag. The name with plugin is necessary, you can ask the maintainer of
rollup-plugin-node-builtins
add this or addname
with yourself. For build error, I pushed the pr to fix it and please wait https://github.com/rollup/plugins/issues/554#issuecomment-680423764 be resolved.@wighawag. This is can be closed. You can track here https://github.com/rollup/rollup/issues/3765 for object prototype is lost .
{ ...builtins({ crypto: true }), name: 'rollup-plugin-node-builtins' }
should do as a workaround