-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
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
Bundling SSR Modules using CommonJS exports alias results in ReferenceError: exports is not defined #2579
Comments
@GrygrFlzr I believe I am running into this issue as well, and had posted an issue with LayerCake. I have a minimal SvelteKit repo here if it helps to troubleshoot. |
are there any workarounds? |
You could try importing the library via ye olde |
Same problem with faunadb. |
It turns out that Rollup creates output that uses just |
I am having exactly the same error with better-sqlite3 . |
Can't solve the problem even using the |
For me changing to |
It looks like doesn't support commomjs |
@rifler, check out my issue: I converted the module from cjs to esm. After that I placed the module directly into my project and removed the dependency. This is really ugly but seems to work for rhis moment. @GrygrFlzr, how are your plans regarding to cjs modules? Will there be a solution or is cjs just not supported? |
#2890 talks about a potential, but ugly work-around. |
@ggoodman I'm not even using vite-cjs-ssr and I get this error
|
Another lesser used syntax |
have you tried import {* as somename} from 'module';
//tried with cookie and it worked
import * as cookie from 'cookie'; |
Managed to get around a similar issue by using the vite-plugin-commonjs plugin: // vite.config.ts
import { esbuildCommonjs, viteCommonjs } from "@originjs/vite-plugin-commonjs";
import react from "@vitejs/plugin-react";
import { UserConfig } from "vite";
import ssr from "vite-plugin-ssr/plugin";
const config: UserConfig = {
plugins: [viteCommonjs(), react(), ssr()],
optimizeDeps: {
esbuildOptions: {
plugins: [esbuildCommonjs(["@app/config"])], // the problematic cjs module
},
include: ["@app/config"], // also here
},
};
export default config; |
This workaround works for me: Inside vite.config.ts
|
I seem to have a related problem with the
|
I was having this issue and fixed with https://github.com/originjs/vite-plugins/tree/main/packages/vite-plugin-commonjs Working on a sveltekit project may help someone in the future |
I am seeing something similar - I'll verify someone else in my company can repro on a windows machine before making an issue. The repo in question is cypress-io/cypress - Could be dup of #8800 |
This issue was closed for vite v3, please check the latest beta. You'll need to add the dep to |
I can't seem to get basic node modules working on the build version, using Works on dev mode. Adding
Gives the error The https://kit.svelte.dev/docs/hooks#handle
Doesn't, out of ideas here... I'm amazed that something this basic is broken.. |
@OllyDixon you put the config in the wrong place. It needs to go in I'm amazed that you broke something this basic 😉 |
I removed all of the config and added the import like this instead |
Wa also able to resolve this issue by using the vite-plugin-commonjs plugin. |
Describe the bug
When importing an SSR module, if
exports
alias instead ofmodule.exports
,ssr.noExternal
is declared with the moduleVite is unable to evaluate it. This can also happen with the more common
module.exports
, but seems to not be guaranteed.While this behavior may seem esoteric due to modern proliferation of
module.exports
, this is a feature of Node since v0.1.16 and has not been deprecated. In addition, there are instances of various npm modules in the wild which use this method of exporting fields, including, but not limited to:...and much, much, more searchable on GitHub. I believe this is also an output of some bundlers.
Reproduction
https://github.com/GrygrFlzr/vite-cjs-ssr
git clone https://github.com/GrygrFlzr/vite-cjs-ssr.git cd vite-cjs-ssr npm i npm run dev
Visit http://localhost:3000 to trigger SSR, which consists of a very simple script that uses the
cookie
npm module.The page will render
exports is not defined
and errors will show up in the server console:System Info
vite
version: 2.1.2The text was updated successfully, but these errors were encountered: