vite-plugin-node-polyfills
vite-plugin-node-polyfills copied to clipboard
Bug: conflict with another middleware plugin
Summary
very helpful plugin, but I seems run into a conflict with an express server plugin.
- brand new react app generate by vite (JavaScript + SWC), version "vite": "^5.2.0", "vite-plugin-node-polyfills": "^0.21.0"
- vite.config.js is
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react-swc'
import { nodePolyfills } from 'vite-plugin-node-polyfills'
function express(path) {
return {
name: "vite-plugin-express",
configureServer: async (server) => {
server.middlewares.use(async (req, res, next) => {
process.env["VITE"] = "true";
try {
const { app } = await server.ssrLoadModule(path);
app(req, res, next);
} catch (err) {
console.error(err);
}
});
},
};
}
export default defineConfig({
plugins: [nodePolyfills(), react(), express('src/server')],
server: {
port: 3000,
},
})
- the src/server.js is
import express from 'express';
//the following 3 lines, (actualy the first line import) will hang the server
/*
import path from 'path';
const rootPath = path.dirname(new URL(import.meta.url).pathname);
console.log(rootPath);
*/
export const app = express();
app.use(express.json());
app.get('/api/hello', (req, res) => {
res.status(200).json({
message: 'hello',
});
});
now, with the 3 lines (import path) commented, everything works. or if I un-comment these 3 lines but remove the nodePolyfills() plugin, everything still works. But If I have the nodePolyfills() plugin, and have import path in the server file, server will hang. the error messages are
12:33:07 AM [vite] Error when evaluating SSR module /node_modules/path-browserify/index.js:
|- ReferenceError: module is not defined
at eval (node_modules/path-browserify/index.js:531:1)
at instantiateModule (file:///node_modules/vite/dist/node/chunks/dep-C-KAszbv.js:55006:15)
12:33:07 AM [vite] Error when evaluating SSR module src/server: failed to import "/node_modules/path-browserify/index.js"
|- ReferenceError: module is not defined
at eval node_modules/path-browserify/index.js:531:1)
at instantiateModule (file:///node_modules/vite/dist/node/chunks/dep-C-KAszbv.js:55006:15)
I have a similar error, but from Astro middleware:
2:42:08 PM [vite] Error when evaluating SSR module /@fs/Users/user/Desktop/projects/project/node_modules/path-browserify/index.js:
|- ReferenceError: module is not defined
at eval (/Users/user/Desktop/projects/project/node_modules/path-browserify/index.js:531:1)
at instantiateModule (file:///Users/user/Desktop/projects/project/node_modules/vite/dist/node/chunks/dep-_QLjGPdL.js:55089:15)
However, the global shims, etc. still work.
I think this error comes from any polyfills that do not support ESModules. If I patch path-browserify to use export default = instead of module.exports = , then the error goes away for the middleware.
Finding alternative polyfills like pathe for path seems to resolve the issue. It would be good to check other polyfills, as some like process also fail.
In some cases using the "exclude" and "overrides" sections can sorta fix this issue.
Adding path: "path-browserify" to overrides solved it for me, server-side on Vercel at least. Using that on my local machine breaks with that missing module error, the only way to fix it is to remove the polyfill plugin as far as I'm aware.