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

TypeORM breaks in Serverless Framework Webpack #8714

Closed
mkilp opened this issue Mar 1, 2022 · 14 comments
Closed

TypeORM breaks in Serverless Framework Webpack #8714

mkilp opened this issue Mar 1, 2022 · 14 comments

Comments

@mkilp
Copy link
Contributor

mkilp commented Mar 1, 2022

Issue Description

TypeORM does not seems to play well while being used in a Serverless Framework application. I'm trying to use it in a Lambda environment to handle all my db things. Error seems to happen after version 0.2.42. I would look into it myself, but I'm sadly not that familiar with the whole webpack world. As being asked in the other issue. Tagging @giladgd as instructed in #8689.

Expected Behavior

TypeORM works fine inside Webpack and Serverless Framework

Actual Behavior

(Own PC paths are redacted and replaced by "SourceRoot")

✖ Cannot destructure property 'ConnectionOptionsReader' of '_index_js__WEBPACK_IMPORTED_MODULE_0___default(...)' as it is undefined.
✖ TypeError: Cannot destructure property 'ConnectionOptionsReader' of '_index_js__WEBPACK_IMPORTED_MODULE_0___default(...)' as it is undefined.
      at Module.../../typeorm/index.mjs (<SourceRoot>/.webpack/service/organizations/organizationHandler.js:74681:5)
      at __webpack_require__ (<SourceRoot>/.webpack/service/organizations/organizationHandler.js:148244:42)
      at Module.../../../libs/databaseHelper.ts (<SourceRoot>/.webpack/service/organizations/organizationHandler.js:71884:65)
      at __webpack_require__ (<SourceRoot>/.webpack/service/organizations/organizationHandler.js:148244:42)
      at <SourceRoot>/.webpack/service/organizations/organizationHandler.js:148329:78
      at <SourceRoot>/.webpack/service/organizations/organizationHandler.js:148548:3
      at Object.<anonymous> (<SourceRoot>/.webpack/service/organizations/organizationHandler.js:148553:12)
      at Module._compile (node:internal/modules/cjs/loader:1103:14)
      at Object.Module._extensions..js (node:internal/modules/cjs/loader:1155:10)
      at Module.load (node:internal/modules/cjs/loader:981:32)
      at Function.Module._load (node:internal/modules/cjs/loader:822:12)
      at Module.require (node:internal/modules/cjs/loader:1005:19)
      at require (node:internal/modules/cjs/helpers:102:18)
      at <SourceRoot>/node_modules/serverless-offline/dist/lambda/handler-runner/in-process-runner/InProcessRunner.js:157:133
      at processTicksAndRejections (node:internal/process/task_queues:96:5)
      at async InProcessRunner.run (<SourceRoot>/node_modules/serverless-offline/dist/lambda/handler-runner/in-process-runner/InProcessRunner.js:157:9)

Steps to Reproduce

  1. Create an empty serverless project
  2. Install serverless-offline (for testing)
  3. Create a central TypeORM database handler outside of your functions
  4. Import the database handler in your function
  5. Run serverless offline -o local
  6. Crash will happen during bundling / transpiling

My Environment

Dependency Version
Operating System WIN 10 / WSL Ubuntu 20.4
Node.js version 16.14
Typescript version 4.5.4
TypeORM version 0.2.44

Relevant Database Driver(s)

DB Type Reproducible
aurora-data-api yes
aurora-data-api-pg no
better-sqlite3 no
cockroachdb no
cordova no
expo no
mongodb no
mysql no
nativescript no
oracle no
postgres no
react-native no
sap no
sqlite no
sqlite-abstract no
sqljs no
sqlserver no

Are you willing to resolve this issue by submitting a Pull Request?

  • ✖️Yes, I have the time, and I know how to start.
  • ✖️ Yes, I have the time, but I don't know how to start. I would need guidance.
  • ✖️ No, I don’t have the time, but I can support (using donations) development.
  • ✅ No, I don’t have the time and I’m okay to wait for the community / maintainers to resolve this issue.
@giladgd
Copy link
Contributor

giladgd commented Mar 1, 2022

@mkilp Please try using version 0.2.45-dev.32549fe (as version 0.2.25 isn't released yet) and report if the issue still persists, as this version seems to include the fix for this issue (#8710)

@mkilp
Copy link
Contributor Author

mkilp commented Mar 1, 2022

@giladgd thanks for the swift response! Still getting the same error as above - that being said for your information I am using serverless-bundle (https://github.com/AnomalyInnovations/serverless-bundle) as my webpack bundler. I setup a repro repo and will try to exclude typeorm from the serverless bundling and see if that is a temporary workaround.

@giladgd
Copy link
Contributor

giladgd commented Mar 2, 2022

@mkilp Can you please provide me with an example repo to reproduce the issue locally?
It would allow me to investigate the issue and find a solution

@mkilp
Copy link
Contributor Author

mkilp commented Mar 2, 2022

@giladgd https://github.com/mkilp/typeorm-webpack-bug here you go. Thanks!

@giladgd
Copy link
Contributor

giladgd commented Mar 2, 2022

@mkilp Since you're using Webpack without the default browser target, you have to explicitly import "typeorm/browser" instead of just "typeorm"

@cognot
Copy link

cognot commented Mar 4, 2022

Hi,

Not sure if it is exactly the same problem, but I was having the same issue, albeit in a different setup. What I have is a server-side database defined with TypeORM, types are declared in a "shared" folder used for server-side AND client-side type consistency. Client-side is going through webpack for bundling.

No database manipulation is done client-side, types are only used to get a consistent type checking between server and client.

Since 0.2.42, importing the types client-side lead to webpack complaining the typeorm import failed, and at runtime the same "Cannot destructure property 'ConnectionOptionsReader" error.

What I ended up doing was adding an alias in my webpack configuration:


const config: webpack.Configuration = {
...
  resolve: {
    alias: {
      typeorm: resolve("./node_modules/typeorm/browser"),
    },
  },
...
}

Don't know if it is the right workaround or not, but at least it got me going...

Regards,
Richard.

@Nightbr
Copy link

Nightbr commented Mar 9, 2022

I also encounter this bug with serverless framework and also using serverless-bundle.

Here is my configuration to alias typeorm and get the browser dist code to be able to bundle it for Lambda functions:

custom:
  bundle:
    sourcemaps: true
    tsConfig: 'tsconfig.app.json'
    linting: true
    packager: yarn
    disableForkTsChecker: true
    ignorePackages:
      - pg-native
    aliases:
      - 'typeorm': '../../../node_modules/typeorm/browser' # see https://github.com/typeorm/typeorm/issues/8714
    forceExclude:
      - aws-sdk

Note: you need to put the path to your node_modules (I am in an Nx monorepo and the package is in 3 subfolders). You could just set - 'typeorm': './node_modules/typeorm/browser'.

Hope this helps.

@ken-tan3
Copy link

ken-tan3 commented Mar 12, 2022

I faced almost the same problem with @mkilp . There the problem seems invocation at import {Connection} from "typeorm"; in handler.ts, just because @Nightbr mentioned /typeorm/browser issue above.
In my case, I succeeded in import {Connection} from "direct_path_to_typeorm_browser(eg: ./node_modules/typeorm/browser)"; in handler.ts.
As a recommendation, you can deploy a single function $ serverless deploy function --function hello. Then, you can bundle and deploy the single function, and you might reach the true ploblem!!

@agrawalsaurav9
Copy link

I am facing same issue and it got resolve with help of aliases:
- 'typeorm': '../../../node_modules/typeorm/browser'
Is this right approch to go ahead with or Is there any solution to handle it by using webpack ?

@agrawalsaurav9
Copy link

I used esbuild to bundle functions instead of webpack.
we can refer:- https://www.serverless.com/plugins/serverless-esbuild

@mkilp
Copy link
Contributor Author

mkilp commented Mar 21, 2023

For me it worked with an alias like mentioned in the comments. Thanks everyone!

@mkilp mkilp closed this as completed Mar 21, 2023
@ricardo-cn
Copy link

Hi everyone. I tried the suggestion with the alises, but now I am getting this error:

serverless-bundle: CommonJS, ES3, or ES5 are not supported
Bundling with Webpack...
ERROR in ../../typeorm/browser/driver/mongodb/bson.typings.js 1:9
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
 @ ../../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
 @ ../../typeorm/browser/index.js 128:0-41 128:0-41

Does anyone have any idea why the BSON error is being generated?

@ricardo-cn
Copy link

If I check the bundled file, it has this line:

/***/ "../../typeorm/browser/driver/mongodb/bson.typings.js":
/*!************************************************************!*\
  !*** ../../typeorm/browser/driver/mongodb/bson.typings.js ***!
  \************************************************************/
/***/ (() => {

throw new Error("Module parse failed: Export 'BSON' is not defined (1:9)\nYou 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\n> export { BSON };\n| \n| //# sourceMappingURL=bson.typings.js.map");

/***/ }),

/***/ "../../typeorm/browser/driver/mongodb/typings.js":
/*!*******************************************************!*\
  !*** ../../typeorm/browser/driver/mongodb/typings.js ***!
  \*******************************************************/
/***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {

"use strict";
__webpack_require__.r(__webpack_exports__);
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */   BSON: () => (/* reexport safe */ _bson_typings__WEBPACK_IMPORTED_MODULE_0__.BSON),
/* harmony export */   BSONRegExp: () => (/* reexport safe */ _bson_typings__WEBPACK_IMPORTED_MODULE_0__.BSONRegExp),
/* harmony export */   BSONSymbol: () => (/* reexport safe */ _bson_typings__WEBPACK_IMPORTED_MODULE_0__.BSONSymbol),
/* harmony export */   BSONType: () => (/* reexport safe */ _bson_typings__WEBPACK_IMPORTED_MODULE_0__.BSONType),
/* harmony export */   Binary: () => (/* reexport safe */ _bson_typings__WEBPACK_IMPORTED_MODULE_0__.Binary),
/* harmony export */   Code: () => (/* reexport safe */ _bson_typings__WEBPACK_IMPORTED_MODULE_0__.Code),
/* harmony export */   DBRef: () => (/* reexport safe */ _bson_typings__WEBPACK_IMPORTED_MODULE_0__.DBRef),
/* harmony export */   Decimal128: () => (/* reexport safe */ _bson_typings__WEBPACK_IMPORTED_MODULE_0__.Decimal128),
/* harmony export */   Double: () => (/* reexport safe */ _bson_typings__WEBPACK_IMPORTED_MODULE_0__.Double),
/* harmony export */   Int32: () => (/* reexport safe */ _bson_typings__WEBPACK_IMPORTED_MODULE_0__.Int32),
/* harmony export */   Long: () => (/* reexport safe */ _bson_typings__WEBPACK_IMPORTED_MODULE_0__.Long),
/* harmony export */   MaxKey: () => (/* reexport safe */ _bson_typings__WEBPACK_IMPORTED_MODULE_0__.MaxKey),
/* harmony export */   MinKey: () => (/* reexport safe */ _bson_typings__WEBPACK_IMPORTED_MODULE_0__.MinKey),
/* harmony export */   ObjectId: () => (/* reexport safe */ _bson_typings__WEBPACK_IMPORTED_MODULE_0__.ObjectId),
/* harmony export */   Timestamp: () => (/* reexport safe */ _bson_typings__WEBPACK_IMPORTED_MODULE_0__.Timestamp),
/* harmony export */   deserialize: () => (/* reexport safe */ _bson_typings__WEBPACK_IMPORTED_MODULE_0__.deserialize),
/* harmony export */   serialize: () => (/* reexport safe */ _bson_typings__WEBPACK_IMPORTED_MODULE_0__.serialize)
/* harmony export */ });
/* harmony import */ var _bson_typings__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./bson.typings */ "../../typeorm/browser/driver/mongodb/bson.typings.js");
/* harmony import */ var _bson_typings__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(_bson_typings__WEBPACK_IMPORTED_MODULE_0__);
/* harmony import */ var _platform_PlatformTools__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ../../platform/PlatformTools */ "../../typeorm/browser/platform/PlatformTools.js");

If I comment out the line where it throw new Error() ... it works.

This feels super weird -- wonder how that got in there and if anyone knows a feasible way to disable/remove that line without needing me to modify the bundled file.

Thanks!

@ricardo-cn
Copy link

Found a way around that.

Downgrade our typeorm to 0.3.14. That should fix the issue.

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

No branches or pull requests

7 participants