-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
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
vite does not seem to support es module that depends on nodejs builtins #728
Comments
I created a vite only repo with both |
Rollup plugins are only run during builds, you need to provide a middleware to the dev server edit: i might check what's up with the build failing |
Ok, thanks for the info. The build is failing because the code expect plugin to have a name field. |
…ly output es module fix vitejs#728
@wighawag. The name with plugin is necessary, you can ask the maintainer of |
|
@wighawag. This is can be closed. You can track here rollup/rollup#3765 for object prototype is lost . |
@underfin I did this in vite.config.js but still getting error import builtins from 'rollup-plugin-node-builtins';
import globals from 'rollup-plugin-node-globals';
const builtinsPlugin = builtins({crypto: true});
builtinsPlugin.name = 'builtins'; // required, see https://github.com/vitejs/vite/issues/728
const globalsPlugin = globals();
globalsPlugin.name = 'globals'; // required, see https://github.com/vitejs/vite/issues/728
module.exports = {
assetsDir: "assets",
outDir: "static",
rollupInputOptions: {
plugins: [
globalsPlugin,
builtinsPlugin,
]
}
} |
This error is already fixed, please wait new version publish. |
@underfin is this published? Can we try in the meantime somehow? |
@frederikhors You can have a try with link local vite. |
@underfin How we link local vite? |
@underfin I'm getting this error: |
Please see entirely this issue. |
@underfin I fixed the above issue with following: import builtins from 'rollup-plugin-node-builtins';
const builtinsPlugin = { ...builtins({ crypto: true }), name: 'rollup-plugin-node-builtins' };
module.exports = {
assetsDir: "assets",
outDir: "static",
rollupInputOptions: {
preserveEntrySignatures: 'strict',
plugins: [
builtinsPlugin,
]
}
} But when trying to link I'm getting following error now |
@underfin any clues for typescript? I just created with
then vite.config.ts
But still,
|
You can't use |
I'm running into issues with this too. The globals plugin seems to break My config looks like:
The error I'm getting is
|
That's a problem in 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. |
yyx990803 I agree.. but pouchdb relies on "events" so no avoiding it... |
Oh, it was additional babel config in package.json from create react app |
Your suggestion is simply not an option in some scenarios. I'm trying to build an app with I was able to get import { NodeGlobalsPolyfillPlugin } from '@esbuild-plugins/node-globals-polyfill'
import { NodeModulesPolyfillPlugin } from '@esbuild-plugins/node-modules-polyfill'
import { build } from 'esbuild'
build({
entryPoints: ['src/index.js'],
target: 'es2020',
bundle: true,
outfile: 'dist/index.js',
define: {
global: 'globalThis'
},
plugins: [
NodeGlobalsPolyfillPlugin({
process: true,
buffer: true,
define: { 'process.env.NODE_ENV': '"production"' }, // https://github.com/evanw/esbuild/issues/660
}),
NodeModulesPolyfillPlugin()
],
}); The same cannot be replicated in evanw/esbuild#779, evanw/esbuild#790 Is it feasible to incorporate the above solution to the problem in |
可以如何配置兼容吗?项目中引入第三方库包含require |
开发环境都好好的,不知道是不是要配置Rollup参数,实在不行构建还是用webpack,vite只用来本地开发 |
How to config vite.js to use node_module libraries in svelte-kit. I installed npm install locomotive-scroll and called it in .svelte file like this import LocomotiveScroll from 'locomotive-scroll'; I get the following error: ReferenceError: document is not defined |
@Easy-Cloud-in This issue is already closed. You may ask questions in Vite Land or your can open a new discussion on https://github.com/vitejs/vite/discussions. |
Leaving this clue here for anyone who's stumbling into similar problems. https://github.com/snowpackjs/rollup-plugin-polyfill-node |
I'm trying to use Twilio Client JS SDK and it relies on node built-ins. There is nothing i can swap it for. How can i make it work ? |
I'm also seeing "ReferenceError: require is not defined" error in the built output from a dependency the app is using https://github.com/graphql/graphiql. Everything is modern JS except for this dep. Is there a way to make commonjs modules work with vite? Here is my config that is not working:
The build is super fast! But my app doesn't work 😅 Any suggestions would be very helpful ❤️ |
This issue has been locked since it has been closed for more than 14 days. If you have found a concrete bug or regression related to it, please open a new bug report with a reproduction against the latest Vite version. If you have any other comments you should join the chat at Vite Land or create a new discussion. |
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
because otherwise I get the following error :
Also when running
npm run dev
it fails with various error indicating the builtins are not properly injectedReproduction
Here is repo that uses
svite
(https://github.com/dominikg/svite/) where you can reproduce the issue in the branchrollup-plugin-node-builtins
: https://github.com/wighawag/svite-typescript-minimal/tree/rollup-plugin-node-builtinsto test runtime error :
System Info
vite
version: 1.0.0-rc.4The text was updated successfully, but these errors were encountered: