Error on mongodb
import { define } from "../../utils.ts";
import { MongoClient } from "npm:[email protected]";
export const handler = define.handlers({
async GET(ctx) {
const name = ctx.params.name;
const password = "1Ik1eA38GBcGqVDd"
const client = new MongoClient(`mongodb+srv://kavanmevada_db_user:${password}@cluster0.jua4ouv.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0`);
await client.connect();
interface DinosaurSchema {
name: string;
skills: string[];
}
const db = client.db("animals");
const dinosaurs = db.collection<DinosaurSchema>("dinosaurs");
await dinosaurs.insertOne({
name: "deno",
skills: ["dancing", "hiding"],
});
const allDinosaurs = await dinosaurs.find({ name: "deno" }).toArray();
console.log(allDinosaurs);
client.close();
return new Response(
`Hello, ${name.charAt(0).toUpperCase() + name.slice(1)}!`,
);
},
});
deno build log:
Task build vite build
vite v7.1.7 building for production...
✓ 25 modules transformed.
_fresh/client/.vite/manifest.json 0.63 kB │ gzip: 0.24 kB
_fresh/client/assets/client-entry-PRjVyscb.css 1.83 kB │ gzip: 0.89 kB
_fresh/client/assets/fresh-island__Counter-lU_Fxzzr.js 0.82 kB │ gzip: 0.50 kB
_fresh/client/assets/preact.module-B5DG9wGp.js 10.91 kB │ gzip: 4.63 kB
_fresh/client/assets/client-entry-BnA5gZOx.js 24.55 kB │ gzip: 9.07 kB
✓ built in 1.38s
vite v7.1.7 building SSR bundle for production...
node_modules/.deno/[email protected]/node_modules/mongodb/lib/bson.js (1:245): "default" is not exported by "node_modules/.deno/[email protected]/node_modules/bson/lib/bson.node.mjs", imported by "node_modules/.deno/[email protected]/node_modules/mongodb/lib/bson.js".
node_modules/.deno/[email protected]/node_modules/mongodb/lib/cmap/auth/mongodb_oidc/callback_workflow.js (1:474): "default" is not exported by "node_modules/.deno/[email protected]/node_modules/bson/lib/bson.node.mjs", imported by "node_modules/.deno/[email protected]/node_modules/mongodb/lib/cmap/auth/mongodb_oidc/callback_workflow.js".
node_modules/.deno/[email protected]/node_modules/mongodb/lib/cmap/auth/mongodb_oidc/service_workflow.js (1:319): "default" is not exported by "node_modules/.deno/[email protected]/node_modules/bson/lib/bson.node.mjs", imported by "node_modules/.deno/[email protected]/node_modules/mongodb/lib/cmap/auth/mongodb_oidc/service_workflow.js".
node_modules/.deno/[email protected]/node_modules/mongodb/lib/mongo_logger.js (1:338): "default" is not exported by "node_modules/.deno/[email protected]/node_modules/bson/lib/bson.node.mjs", imported by "node_modules/.deno/[email protected]/node_modules/mongodb/lib/mongo_logger.js".
On visiting endpoint:
GET http://0.0.0.0:8000/api/asd
TypeError: (0 , sparse_bitfield_1.default) is not a function
at read (file:///home/kavan/Projects/fresh-project/_fresh/server/assets/_fresh-route___api_name_-CuFXV8Wx.mjs:10996:42)
at createMemoryCodePoints (file:///home/kavan/Projects/fresh-project/_fresh/server/assets/_fresh-route___api_name_-CuFXV8Wx.mjs:10998:34)
at file:///home/kavan/Projects/fresh-project/_fresh/server/assets/_fresh-route___api_name_-CuFXV8Wx.mjs:11100:68
Try excluding MongoDB from Vite's SSR bundling, optimizeDeps bundling, and finally exclude it from rollup's bundling. Add this to your vite config.
For my configuration, I've made a serverSideModules an array to exclude any other modules that you'll only be using serverside only.
The configuration below will make sure that MongoDB is excluded in bundling for both development and production environments.
//vite.config.ts
const serverSideModules = [ "mongodb" ];
export default defineConfig({
//Your other configurations
ssr: {
external: serverSideModules
},
optimizeDeps: {
exclude: serverSideModules
},
build: {
rollupOptions: {
external: serverSideModules
}
}
});
Good luck, and hope this helps!
Thanks, It helped a lot.
Reopening because user's shouldn't need to change the vite config to make stuff work.
Why do we need to include everything by default in ssr bundle? Wouldn't it be better for SSR to only include current project, leaving transformations of everything else to Deno?
I understand the concern with possible preact package duplication, but it seems like bundling everything is too much and would result in game of whack-o-mole given how many hacks many of the nodejs backend packages employ, and package deduplication is better served by e.g creating deno workspace diagnostics instead
I would love nothing more than to be able to use vite's built in external stuff. But I always ran into issues with duplicate version of Preact which lead to broken apps. It's not a harmless issue, it leads to broken apps that fail with cryptic JS errors. The current situation is far from ideal, but it is the lesser of the two evils for the time being as it leads to less apps being broken.
When I migrate to Fresh using Vite, I have to made some dependency as external because some dependencies .wasm file or font-file don't properly copied into outDir. But, later i've just use vite-plugin-static-copy to copy those files. And then just apply those plugin for 'ssr' environment. https://github.com/mblonyox/peraturan.info/blob/fbba538d76fac75ffb2f2ae1b1e437e573b280a1/vite.config.ts#L10-L29
Was investigating similar issue for other package, and while the problem described in main post is easily solvable by adding __esModule export unconditionally for transpiled module (Because mongodb and some other packages use function importDefault(mod) { if ('__esModule' in mod) return mod else return {default: mod} }), and after transpilation the module is technically always es modules due to how exports are formed:
--- a/packages/plugin-vite/src/plugins/patches/commonjs.ts
+++ b/packages/plugin-vite/src/plugins/patches/commonjs.ts
@@ -358,11 +358,7 @@ export function cjsPlugin(
]),
),
);
- }
- if (body.length === 0 && hasEsModule) {
- path.pushContainer("body", t.exportNamedDeclaration(null));
- } else if (hasEsModule) {
path.pushContainer(
"body",
t.exportNamedDeclaration(
@@ -370,15 +366,16 @@ export function cjsPlugin(
"var",
[t.variableDeclarator(
t.identifier("__esModule"),
- t.memberExpression(
- t.identifier("exports"),
- t.identifier("__esModule"),
- ),
+ // __esModule is a babel-proposed transpilation flag, which should never be falsy
+ t.booleanLiteral(true),
)],
),
),
);
}
},
},
CallExpression(path, state) {
There is another problem: mongodb itself and multiple other popular npm packages are doing something very stupid by using https://github.com/addaleax/gen-esm-wrapper:
import mod from "./lib/index.js";
export default mod["default"];
export const CommaAndColonSeparatedRecord = mod.CommaAndColonSeparatedRecord;
export const ConnectionString = mod.ConnectionString;
export const redactConnectionString = mod.redactConnectionString;
It won't work the way current commonjs transform is implemented in fresh plugin, just because this is stupid, and only works when there is a difference between commonjs and ESM module imports, and I wonder why someone thought it is a good idea.
I don't see any way out of this, other than special-casing some of the modules, i.e .esm-wrapper.mjs for mongodb and bson packages
Patching .esm-wrapper.mjs to rewrite import mod to import * from mod together with #3494 seems to fix this, #3449, #3505, and possibly some other issues.