Skip to content
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

NextJS client component: Module parse failed: Export 'BSON' is not defined #9959

Closed
18 tasks
argaen opened this issue Apr 18, 2023 · 31 comments · Fixed by #9962 · 4 remaining pull requests
Closed
18 tasks

NextJS client component: Module parse failed: Export 'BSON' is not defined #9959

argaen opened this issue Apr 18, 2023 · 31 comments · Fixed by #9962 · 4 remaining pull requests

Comments

@argaen
Copy link

argaen commented Apr 18, 2023

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.tsx and 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.

@spotykatch
Copy link
Contributor

spotykatch commented Apr 18, 2023

You are trying to run typeorm on client side.
You may either create a separate API app or place data source initializer somewhere in lib folder, for example (not pages or app folder) and call it inside API Routes or create Custom Server. Try to find examples in their GitHub
I'm not sure about API routes. Doing so can course problems with connections that don't close.

Ooops did'n see you're using sqljs

@EinfachHans
Copy link

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 (from 0.3.14)

@spotykatch
Copy link
Contributor

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 (from 0.3.14)

In your case, it seems like a bug caused by PR #9925. Please make an issue.

@pleerock
Copy link
Member

let's ask @mptr for help here.

I do see export { BSON } line which exports namespace... not sure why this breaks for some users... do you have any ideas?

@jorge-menjivar
Copy link

I am having the same issue here. However, this is my first time using typeorm so I am not sure if this is a misconfiguration issue from my part.

@spotykatch
Copy link
Contributor

spotykatch commented Apr 18, 2023

@argaen If I'm not mistaken you can import typeorm like so to go most of the errors away. Except BSON one.
import { DataSource } from 'typeorm/browser';

@pleerock, maybe replace browser/driver/mongodb/typings.js with dummy in package.json? I don't think we need MongoDB stuff in browser.

@pleerock
Copy link
Member

@spotykatch yes we can do it, but we also need to ensure problem is with browser version.

Would be great if someone can replace the typings to dummy once & test it, and create a PR with the changes.

spotykatch added a commit to spotykatch/typeorm that referenced this issue Apr 18, 2023
@spotykatch
Copy link
Contributor

@pleerock I can't test it from my current location, can you please launch CircleCI on the pr?

@pleerock
Copy link
Member

I don't think CI can test this change. Manual test is required.

@spotykatch
Copy link
Contributor

Ok, I'll switch it back to pr when I'm done

@spotykatch
Copy link
Contributor

The error is still there. Gulp compiles dummy to platform folder, but the original file is not replaced. I'm definitely doing something wrong.

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
./lib/database.ts
./app/page.tsx

@spotykatch
Copy link
Contributor

Need help, @imnotjames, @chriswep, @daniel-lang.

@argaen
Copy link
Author

argaen commented Apr 19, 2023

Just for the sake of trying, I replaced the module with the following:

const BSON = {};
export { BSON };

//# sourceMappingURL=bson.typings.js.map

But seems the types are needed even though I'm using sql.js driver:

export 'Binary' (reexported as 'Binary') was not found in './bson.typings' (possible exports: BSON)

Import trace for requested module:
./node_modules/typeorm/browser/driver/mongodb/typings.js
./node_modules/typeorm/browser/index.js
./src/app/page.tsx

./node_modules/typeorm/browser/driver/mongodb/typings.js
export 'BSONRegExp' (reexported as 'BSONRegExp') was not found in './bson.typings' (possible exports: BSON)

Import trace for requested module:
./node_modules/typeorm/browser/driver/mongodb/typings.js
./node_modules/typeorm/browser/index.js
./src/app/page.tsx

./node_modules/typeorm/browser/driver/mongodb/typings.js
export 'BSONSymbol' (reexported as 'BSONSymbol') was not found in './bson.typings' (possible exports: BSON)

Import trace for requested module:
./node_modules/typeorm/browser/driver/mongodb/typings.js
./node_modules/typeorm/browser/index.js
./src/app/page.tsx

./node_modules/typeorm/browser/driver/mongodb/typings.js
export 'BSONType' (reexported as 'BSONType') was not found in './bson.typings' (possible exports: BSON)

Import trace for requested module:
./node_modules/typeorm/browser/driver/mongodb/typings.js
./node_modules/typeorm/browser/index.js
./src/app/page.tsx

./node_modules/typeorm/browser/driver/mongodb/typings.js
export 'Code' (reexported as 'Code') was not found in './bson.typings' (possible exports: BSON)

Import trace for requested module:
./node_modules/typeorm/browser/driver/mongodb/typings.js
./node_modules/typeorm/browser/index.js
./src/app/page.tsx

./node_modules/typeorm/browser/driver/mongodb/typings.js
export 'DBRef' (reexported as 'DBRef') was not found in './bson.typings' (possible exports: BSON)

[...]

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.

@awesomedan93
Copy link

Same issue for me...

spotykatch added a commit to spotykatch/typeorm that referenced this issue Apr 21, 2023
spotykatch added a commit to spotykatch/typeorm that referenced this issue Apr 21, 2023
Closes: typeorm#9959

fix: delete unnessecary dummy file
@spotykatch
Copy link
Contributor

spotykatch commented Apr 22, 2023

Please check this test repository. That uses future build (I guess).
Take a look at webpack config also. If you use React Native, you will have other config.

@spotykatch
Copy link
Contributor

Can someone help me with another test? I'm pretty sure it solves the BSON problem, because without changes it throws that error, but it still does not work with vite. codesandbox

@argaen
Copy link
Author

argaen commented Apr 29, 2023

Hi! thanks for this @spotykatch. I was browsing through the repo, not 100% sure which changes I should apply to my Next.JS app. To start I just added the normalmodulereplacement in my next config:

/** @type {import('next').NextConfig} */
const nextConfig = {
  reactStrictMode: true,
  experimental: {
    appDir: true,
  },
  webpack: (config, { isServer, webpack }) => {
    if (!isServer) {
      config.resolve.fallback.fs = false;
    }
    config.plugins = [
      new webpack.NormalModuleReplacementPlugin(/typeorm$/, function (result) {
        result.request = result.request.replace(/typeorm/, "typeorm/browser");
      }),
    ];

    return config;
  },
}

module.exports = nextConfig

When I do the change I get an error though:

info  - Thank you for testing `appDir` please leave your feedback at https://nextjs.link/app-feedback
error - ./node_modules/next/dist/build/webpack/loaders/next-client-pages-loader.js?absolutePagePath=next%2Fdist%2Fpages%2F_app&page=%2F_app!
TypeError: Cannot read properties of undefined (reading 'traceChild')
wait  - compiling...
error - ./node_modules/next/dist/build/webpack/loaders/next-client-pages-loader.js?absolutePagePath=next%2Fdist%2Fpages%2F_app&page=%2F_app!
TypeError: Cannot read properties of undefined (reading 'traceChild')
wait  - compiling /dashboard/home/page (client and server)...
error - ./node_modules/next/dist/pages/_app.js
TypeError: Cannot read properties of undefined (reading 'traceChild')
wait  - compiling /_error (client and server)...
error - ./node_modules/next/dist/build/webpack/loaders/next-client-pages-loader.js?absolutePagePath=%2FUsers%2Fmanuelmiranda%2FProjects%2Fmaffin%2Fnode_modules%2Fnext%2Fdist%2Fpages%2F_error.js&page=%2F_error!
TypeError: Cannot read properties of undefined (reading 'traceChild')
error - Error: Cannot find module '/Users/manuelmiranda/Projects/maffin/.next/fallback-build-manifest.json'
Require stack:
- /Users/manuelmiranda/Projects/maffin/node_modules/next/dist/server/load-components.js
- /Users/manuelmiranda/Projects/maffin/node_modules/next/dist/server/next-server.js
- /Users/manuelmiranda/Projects/maffin/node_modules/next/dist/server/dev/next-dev-server.js
- /Users/manuelmiranda/Projects/maffin/node_modules/next/dist/server/next.js
- /Users/manuelmiranda/Projects/maffin/node_modules/next/dist/server/lib/render-server.js
- /Users/manuelmiranda/Projects/maffin/node_modules/next/dist/compiled/jest-worker/processChild.js
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:956:15)
    at Function.mod._resolveFilename (/Users/manuelmiranda/Projects/maffin/node_modules/next/dist/build/webpack/require-hook.js:23:32)
    at Function.Module._load (node:internal/modules/cjs/loader:804:27)
    at Module.require (node:internal/modules/cjs/loader:1028:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.loadDefaultErrorComponentsImpl [as loadDefaultErrorComponents] (/Users/manuelmiranda/Projects/maffin/node_modules/next/dist/server/load-components.js:23:24)
    at DevServer.getFallbackErrorComponents (/Users/manuelmiranda/Projects/maffin/node_modules/next/dist/server/dev/next-dev-server.js:1265:47)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async DevServer.renderErrorToResponseImpl (/Users/manuelmiranda/Projects/maffin/node_modules/next/dist/server/base-server.js:1455:40)
    at async DevServer.pipeImpl (/Users/manuelmiranda/Projects/maffin/node_modules/next/dist/server/base-server.js:548:25)
    at async Object.fn (/Users/manuelmiranda/Projects/maffin/node_modules/next/dist/server/next-server.js:1030:21)
    at async Router.execute (/Users/manuelmiranda/Projects/maffin/node_modules/next/dist/server/router.js:297:32)
    at async DevServer.runImpl (/Users/manuelmiranda/Projects/maffin/node_modules/next/dist/server/base-server.js:522:29)
    at async DevServer.run (/Users/manuelmiranda/Projects/maffin/node_modules/next/dist/server/dev/next-dev-server.js:869:20)
    at async DevServer.handleRequestImpl (/Users/manuelmiranda/Projects/maffin/node_modules/next/dist/server/base-server.js:456:20) {
  digest: undefined
}
Error: Cannot find module '/Users/manuelmiranda/Projects/maffin/.next/fallback-build-manifest.json'
Require stack:
- /Users/manuelmiranda/Projects/maffin/node_modules/next/dist/server/load-components.js
- /Users/manuelmiranda/Projects/maffin/node_modules/next/dist/server/next-server.js
- /Users/manuelmiranda/Projects/maffin/node_modules/next/dist/server/dev/next-dev-server.js
- /Users/manuelmiranda/Projects/maffin/node_modules/next/dist/server/next.js
- /Users/manuelmiranda/Projects/maffin/node_modules/next/dist/server/lib/render-server.js
- /Users/manuelmiranda/Projects/maffin/node_modules/next/dist/compiled/jest-worker/processChild.js
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:956:15)
    at Function.mod._resolveFilename (/Users/manuelmiranda/Projects/maffin/node_modules/next/dist/build/webpack/require-hook.js:23:32)
    at Function.Module._load (node:internal/modules/cjs/loader:804:27)
    at Module.require (node:internal/modules/cjs/loader:1028:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.loadDefaultErrorComponentsImpl [as loadDefaultErrorComponents] (/Users/manuelmiranda/Projects/maffin/node_modules/next/dist/server/load-components.js:23:24)
    at DevServer.getFallbackErrorComponents (/Users/manuelmiranda/Projects/maffin/node_modules/next/dist/server/dev/next-dev-server.js:1265:47)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async DevServer.renderErrorToResponseImpl (/Users/manuelmiranda/Projects/maffin/node_modules/next/dist/server/base-server.js:1455:40)
    at async pipe.req.req (/Users/manuelmiranda/Projects/maffin/node_modules/next/dist/server/base-server.js:1326:30)
    at async DevServer.pipeImpl (/Users/manuelmiranda/Projects/maffin/node_modules/next/dist/server/base-server.js:548:25)
    at async DevServer.run (/Users/manuelmiranda/Projects/maffin/node_modules/next/dist/server/dev/next-dev-server.js:877:28)
    at async DevServer.handleRequestImpl (/Users/manuelmiranda/Projects/maffin/node_modules/next/dist/server/base-server.js:456:20) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/Users/manuelmiranda/Projects/maffin/node_modules/next/dist/server/load-components.js',
    '/Users/manuelmiranda/Projects/maffin/node_modules/next/dist/server/next-server.js',
    '/Users/manuelmiranda/Projects/maffin/node_modules/next/dist/server/dev/next-dev-server.js',
    '/Users/manuelmiranda/Projects/maffin/node_modules/next/dist/server/next.js',
    '/Users/manuelmiranda/Projects/maffin/node_modules/next/dist/server/lib/render-server.js',
    '/Users/manuelmiranda/Projects/maffin/node_modules/next/dist/compiled/jest-worker/processChild.js'
  ]
}

@spotykatch
Copy link
Contributor

Thanks for the question @argaen. It helped me to figure out that pr does not work for next. Here's my webpack config

// ...
webpack: (config, { webpack }) => {
  config.resolve.fallback = {
    ...config.resolve.fallback,
    fs: false,
    crypto: false,
    path: false,
    "react-native-sqlite-storage": false,
    "aws-sdk": false
  };
  config.plugins = [
    ...config.plugins,
    new webpack.NormalModuleReplacementPlugin(/typeorm$/, function (result) {
      result.request = result.request.replace(/typeorm/, "typeorm/browser");
    }),
    new webpack.ProvidePlugin({
      "window.SQL": "sql.js/dist/sql-wasm.js",
    }),
  ];
  return config;
},
// ...

@argaen
Copy link
Author

argaen commented Apr 30, 2023

Thanks for sharing, I've just tried this config in my project and still same result:

/** @type {import('next').NextConfig} */
const nextConfig = {
  reactStrictMode: true,
  experimental: {
    appDir: true,
  },
  webpack: (config, { isServer, webpack }) => {
    if (!isServer) {
      config.resolve.fallback = {
        ...config.resolve.fallback,
        fs: false,
        crypto: false,
        path: false,
        "react-native-sqlite-storage": false,
        "aws-sdk": false
      };

      config.plugins = [
        ...config.plugins,
        new webpack.NormalModuleReplacementPlugin(/typeorm$/, function (result) {
          result.request = result.request.replace(/typeorm/, "typeorm/browser");
        }),
      ];
    }

    return config;
  },
}

module.exports = nextConfig
./src/app/dashboard/home/page.tsx
wait  - compiling /_error (client and server)...
error - ./node_modules/typeorm/browser/driver/mongodb/bson.typings.js
Module parse failed: Export 'BSON' is not defined (2: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
| // const BSON = {};
> export { BSON };
|
| //# sourceMappingURL=bson.typings.js.map

@franzos
Copy link

franzos commented May 1, 2023

I'm seeing a similar issue with a Nuxt.js frontend:

ERROR  in ../node_modules/.pnpm/typeorm@0.3.15/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

 @ ../node_modules/.pnpm/typeorm@0.3.15/node_modules/typeorm/browser/driver/mongodb/typings.js 1:0-188 3:0-18 4:0-16 5:0-22 6:0-22 7:0-20 8:0-16 10:0-17 11:0-22 12:0-23 13:0-18 14:0-17 15:0-16 16:0-18 20:0-18 21:0-20 22:0-21 23:0-21
 @ ../node_modules/.pnpm/typeorm@0.3.15/node_modules/typeorm/browser/index.js

EDIT

Downgrading to the previous version resolves the issue.

"pnpm": {
    "overrides": {
        "typeorm": "0.3.14"
    }
}

@argaen
Copy link
Author

argaen commented May 4, 2023

I've tried to downgrade to 0.3.14 but I still get the same error in Next.js app 🤔

@spotykatch anything you would like me to test? How can I help?

@franzos
Copy link

franzos commented May 4, 2023

@argaen are you sure you're actually using 0.3.14? As far as I can see, BSON was added here (after 0.3.14).

@argaen
Copy link
Author

argaen commented May 5, 2023

Ohh you're right @franzos, some dependencies caching issue. Got it working now so confirming that 0.3.14 fixes it. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment