fresh icon indicating copy to clipboard operation
fresh copied to clipboard

mongoose (CJS) import error

Open fry69 opened this issue 3 months ago • 0 comments

I am not sure if this is not already a known issue, but since it got mentioned in #3500 I think it is good to document it properly:

When trying to use Fresh 2 and Vite with mongoose, which gets distributed as CommonJS, it will fail when using Deno's automatic CommonJS import. At least in conjunction with Vite:

$ deno task build
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-D1ibAoFr.css           6.92 kB │ gzip: 2.27 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-APY_tf7M.js           24.59 kB │ gzip: 9.10 kB
✓ built in 609ms
vite v7.1.7 building SSR bundle for production...
node_modules/.deno/[email protected]/node_modules/mongodb/lib/bson.js (1:536): "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/bson.js (1:584): "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/mongoose/lib/helpers/clone.js (1:1200): "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/mongoose/lib/helpers/clone.js".
node_modules/.deno/[email protected]/node_modules/mongoose/lib/internal.js (3:209): "default" is not exported by "node_modules/.deno/[email protected]/node_modules/mongoose/lib/stateMachine.js", imported by "node_modules/.deno/[email protected]/node_modules/mongoose/lib/internal.js".
node_modules/.deno/[email protected]/node_modules/mongoose/lib/cast/double.js (1:311): "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/mongoose/lib/cast/double.js".
node_modules/.deno/[email protected]/node_modules/mongoose/lib/schema.js (3:2618): "middlewareFunctions" is not exported by "node_modules/.deno/[email protected]/node_modules/mongoose/lib/helpers/model/applyHooks.js", imported by "node_modules/.deno/[email protected]/node_modules/mongoose/lib/schema.js".
node_modules/.deno/[email protected]/node_modules/mongoose/lib/index.js (3:383): "default" is not exported by "node_modules/.deno/[email protected]/node_modules/mongoose/lib/mongoose.js", imported by "node_modules/.deno/[email protected]/node_modules/mongoose/lib/index.js".
✓ 628 modules transformed.
_fresh/server/.vite/manifest.json                               0.73 kB
_fresh/server/assets/_fresh-route___api_name_-B-DPzOeb.mjs      0.95 kB
_fresh/server/assets/_fresh-route___index-8ISxcalF.mjs         13.76 kB
_fresh/server/server-entry.mjs                              2,952.25 kB
✓ built in 5.01s

It works when using this workaround to import this package using Module.createRequire:

$ deno task build
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-D1ibAoFr.css           6.92 kB │ gzip: 2.27 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-APY_tf7M.js           24.59 kB │ gzip: 9.10 kB
✓ built in 636ms
vite v7.1.7 building SSR bundle for production...
✓ 180 modules transformed.
_fresh/server/.vite/manifest.json                             0.73 kB
_fresh/server/assets/_fresh-route___api_name_-PwR9x_mZ.mjs    0.51 kB
_fresh/server/assets/_fresh-route___index-Cn7Uyn6V.mjs       13.32 kB
_fresh/server/server-entry.mjs                              211.26 kB
✓ built in 1.26s

Here is the relevant code snippet that switches between those two modes:

// import mongoose from "mongoose"; // This will not work with Vite

// Workaround for using mongoose with Vite
// Reference: https://github.com/Automattic/mongoose#importing
import { createRequire } from "node:module";

const require = createRequire(import.meta.url);
const mongoose = require("mongoose");

Reproduction repo: https://github.com/fry69/fresh-mongoose

Maybe related issue: #3478

fry69 avatar Sep 28 '25 09:09 fry69