typeorm: NextJS client component: `Module parse failed: Export 'BSON' is not defined`
Issue description
Trying to import DataSource in a NextJS client component breaks with BSON typing import
Expected Behavior
To be able to import the datasource without issues
Actual Behavior
Fails with:
error - ./node_modules/typeorm/browser/driver/mongodb/bson.typings.js
Module parse failed: Export 'BSON' is not defined (1:9)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
> export { BSON };
|
| //# sourceMappingURL=bson.typings.js.map
Import trace for requested module:
./node_modules/typeorm/browser/driver/mongodb/bson.typings.js
./node_modules/typeorm/browser/driver/mongodb/typings.js
./node_modules/typeorm/browser/index.js
./src/app/page.tsx
Steps to reproduce
- Create new project with
yarn create next-app - Install typeorm with
yarn add typeorm - Open
src/app/page.tsxand add the following:
'use client';
import "reflect-metadata";
import { DataSource } from "typeorm/browser";
export async function initDB(database: Uint8Array) {
const datasource = new DataSource({
type: 'sqljs',
synchronize: false,
database,
logging: true,
autoSave: true,
entities: [],
subscribers: [],
migrations: [],
});
await datasource.initialize();
return datasource;
}
No need to call the function. This gives the following error:
error - ./node_modules/typeorm/browser/driver/mongodb/bson.typings.js
Module parse failed: Export 'BSON' is not defined (1:9)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
> export { BSON };
|
| //# sourceMappingURL=bson.typings.js.map
Import trace for requested module:
./node_modules/typeorm/browser/driver/mongodb/bson.typings.js
./node_modules/typeorm/browser/driver/mongodb/typings.js
./node_modules/typeorm/browser/index.js
./src/app/page.tsx
My Environment
package.json
{
"name": "typeormtest",
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint"
},
"dependencies": {
"@types/node": "18.15.11",
"@types/react": "18.0.37",
"@types/react-dom": "18.0.11",
"eslint": "8.38.0",
"eslint-config-next": "13.3.0",
"next": "13.3.0",
"react": "18.2.0",
"react-dom": "18.2.0",
"typeorm": "^0.3.15", # Also tried with 0.3.14
"typescript": "5.0.4"
}
}
node version: 16.17.0
Additional Context
Seems someone else is getting this issue: https://stackoverflow.com/questions/76025976/typeorm-throws-a-bson-error-in-next-js-app
I’m not selecting any specific driver coz even though I’m trying to use sqljs, the driver failing is the mongo one.
Relevant Database Driver(s)
- aurora-mysql
- aurora-postgres
- better-sqlite3
- cockroachdb
- cordova
- expo
- mongodb
- mysql
- nativescript
- oracle
- postgres
- react-native
- sap
- spanner
- sqlite
- sqlite-abstract
- sqljs
- sqlserver
Are you willing to resolve this issue by submitting a Pull Request?
Yes, I have the time, but I don’t know how to start. I would need guidance.
About this issue
- Original URL
- State: closed
- Created a year ago
- Reactions: 5
- Comments: 31 (16 by maintainers)
Commits related to this issue
- fix: exclude mongodb typings from browser Closes: #9959 — committed to spotykatch/typeorm by spotykatch a year ago
- fix: exclude mongodb typings from browser Closes: #9959 — committed to spotykatch/typeorm by spotykatch a year ago
- fix: exclude mongodb typings from browser Closes: #9959 fix: delete unnessecary dummy file — committed to spotykatch/typeorm by spotykatch a year ago
- fix: mongodb typings breaks the browser version (#9962) * fix: exclude mongodb typings from browser Closes: #9959 fix: delete unnessecary dummy file * another fix for bson typings problem in... — committed to typeorm/typeorm by spotykatch a year ago
- Update dependency typeorm to v0.3.16 (#237) This PR contains the following updates: | Package | Type | Update | Change | |---|---|---|---| | [typeorm](https://typeorm.io) ([source](https://github.co... — committed to Vylpes/vylbot-app by deleted user a year ago
- feat: Synced parent changes (#9) * feat: added opaque types support over primitives in find-options (#9560) * added opaque types support over primitives in find-options * removed lock-verify beca... — committed to streamyard/typeorm by javierdelafuentesales a year ago
Same error here. We use typeorm in combination with a local sql lite database in capacitor for building an ios app, android app and electron app. Happens after updating to
0.3.15(from0.3.14)I also encountered this problem when using vue3, electron, sqlite, and vite. Has this problem been solved?
I am still facing this problem. see: https://github.com/Baumgaer/boilerplate/actions/runs/5210172033/jobs/9400922498?pr=54
Ohh you’re right @franzos, some dependencies caching issue. Got it working now so confirming that
0.3.14fixes it. Thanks!I’m seeing a similar issue with a Nuxt.js frontend:
EDIT
Downgrading to the previous version resolves the issue.
Same issue for me…
Just for the sake of trying, I replaced the module with the following:
But seems the types are needed even though I’m using
sql.jsdriver:EDIT: Note that despite all the errors I can still execute my code this way but yeah definitely not something that can do in production.
Need help, @imnotjames, @chriswep, @daniel-lang.