Replies: 13 comments 36 replies
-
How can I make it work with typescript? |
Beta Was this translation helpful? Give feedback.
-
I could only make this solution work by adding the injection to global, i.e. When I added this to my project's index.ts, it didn't work. I think vitejs loads all imports before executing any code, like the snippet above. So |
Beta Was this translation helpful? Give feedback.
-
Hi, do you know how I can do this with with SvelteKit? |
Beta Was this translation helpful? Give feedback.
-
Adding Buffer to window.Buffer worked great for the live server. However, I had problems with the prod build where the entry point script was not being loaded fast enough and the troublesome ledger package prevented the app from loading. Solved with the rollup inject plugin: import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import path from 'path'
import inject from '@rollup/plugin-inject'
export default defineConfig({
plugins: [vue()],
resolve: {
alias: {
'@': path.resolve(__dirname, 'src'),
}
},
build: {
rollupOptions: {
plugins: [inject({ Buffer: ['Buffer', 'Buffer'] })],
},
},
}) |
Beta Was this translation helpful? Give feedback.
-
even easier......add support for esbuild plugins for dev in addition to rollup plugins for prod, where there is already a fantastic node polyfill plugin that includes typedefs: https://www.npmjs.com/package/@esbuild-plugins/node-modules-polyfill |
Beta Was this translation helpful? Give feedback.
-
This no longer seems to work (original post). It produces the following error when running vite dev:
|
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
The original solution wasn't working for me, so I checked the polyfill docs and noticed that you need a trailing slash when importing import { Buffer } from "buffer/";
window.Buffer = Buffer; |
Beta Was this translation helpful? Give feedback.
-
This is how I've made this work with plotly.js, it seems more robust than other solutions: npm add node-stdlib-browser
npm add -D vite-plugin-node-stdlib-browser // vite.config.js
import { defineConfig } from 'vite'
import nodePolyfills from 'vite-plugin-node-stdlib-browser'
export default defineConfig({
// other options
plugins: [nodePolyfills()]
}) It uses this open-source vite plugin. |
Beta Was this translation helpful? Give feedback.
-
For me using the
export default defineConfig({
// your config
resolve: {
alias: {
buffer: 'buffer/',
}
},
}) |
Beta Was this translation helpful? Give feedback.
-
I changed to
|
Beta Was this translation helpful? Give feedback.
-
the way I got it to work is:
|
Beta Was this translation helpful? Give feedback.
-
I just spent a couple hours troubleshooting issues: This plugin worked for me:
|
Beta Was this translation helpful? Give feedback.
-
I’m just playing with porting a Snowpack app to Vite and thought I’d share something I found in case it helps anyone else.
If your client-side code uses Buffer (e.g., you’re doing stuff with cryptography and you’re using a Node.js library) and you get a “Buffer is not a function”-type error, you don’t have to mess with rollup plugins, etc. (which I went down a rabbit-hole with, unsuccessfully).
Instead, install Feross’s Buffer polyfill:
And, in your JS entrypoint (e.g., index.js), do something like this:
And it should work.
Similarly, you can implement other Node polyfills directly in your code, if and when you need them.
(In Snowpack, there is a
polyfillNode
option you can use in the configuration that should do this for you automatically but there’s been a recent regression so I took it as an opportunity to try out Vite. Given that Vite doesn’t have the same directory-related issues and – with this small workaround – seems to just work in my tests, I’m going to start using it instead for our Small Web stuff. Also, expect a Vite + Svelte template for Site.js soon… I actually encountered the issues while working on a Snowpack version, which I’ll release once/if the regressions are fixed.)Beta Was this translation helpful? Give feedback.
All reactions