Open railty opened 3 months ago
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.
Summary
very helpful plugin, but I seems run into a conflict with an express server plugin.
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, }, })
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', }); });
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)