francescov1 / mongoose-tsgen

A plug-n-play Typescript generator for Mongoose.
103 stars 24 forks source link

ESM Support #139

Closed joshgachnang closed 7 months ago

joshgachnang commented 7 months ago

Hello! First off thank you for the awesome package. We've been using it for a couple years and it's been wonderful.

We've just started migrating our repo to ESM. We keep running into packages that only support ESM, so we started the conversion. The last remaining thing is I can't get mongoose-tsgen to work with ESM. Any ideas?

I made an example repo here: https://github.com/joshgachnang/tsgen-esm

Running via npx, I get:

npx mtgen --debug                                              
Debug mode enabled
Generating mongoose typescript definitions... !
    SyntaxError: Cannot use import statement outside a module

So I tried running it from my own script (mongooseTSGen.ts copied into the example repo), and get a little more info:

$ node --loader ts-node/esm ./src/scripts/mongooseTSGen.ts
(node:50562) ExperimentalWarning: `--experimental-loader` may be removed in the future; instead use `register()`:
--import 'data:text/javascript,import { register } from "node:module"; import { pathToFileURL } from "node:url"; register("ts-node/esm", pathToFileURL("./"));'
(Use `node --trace-warnings ...` to show where the warning was created)
Generating Mongoose definitions...
Error generating Mongoose definitions: /Users/josh/src/flourish/backend/src/models/alert.ts:1
import { APIError, logger } from "ferns-api";
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at internalCompileFunction (node:internal/vm:73:18)
    at wrapSafe (node:internal/modules/cjs/loader:1274:20)
    at Module._compile (node:internal/modules/cjs/loader:1320:27)
    at Module.m._compile (/Users/josh/src/flourish/backend/node_modules/mongoose-tsgen/node_modules/ts-node/src/index.ts:1056:23)
    at Module._extensions..js (node:internal/modules/cjs/loader:1414:10)
    at Object.require.extensions.<computed> [as .ts] (/Users/josh/src/flourish/backend/node_modules/mongoose-tsgen/node_modules/ts-node/src/index.ts:1059:12)
    at Module.load (node:internal/modules/cjs/loader:1197:32)
    at Function.Module._load (node:internal/modules/cjs/loader:1013:12)
    at Module.require (node:internal/modules/cjs/loader:1225:19)
    at require (node:internal/modules/helpers:177:18) /Users/josh/src/flourish/backend/src/models/alert.ts:1

Any thoughts here?

francescov1 commented 7 months ago

Hey @joshgachnang, I was able to find a fix to your problem!

This project uses ts-node to read the TS code and parse out certain type information from the Mongoose schemas. Turns out ts-node does not play well with ESNext, see here: https://github.com/TypeStrong/ts-node/issues/922#issuecomment-673155000.

As a fix, you can add the following to your tsconfig.json file:

"ts-node": {
    "compilerOptions": {
      "module": "commonjs"
    }
  }

I tested this on your example repo and it solved the issue.

That being said, I'd rather not require users to need to change their tsconfig.json files anytime they want to use ESNext, so ill be pushing a fix shortly which should handle this from my end, so feel free to wait for that as well

francescov1 commented 7 months ago

Fixed in v9.2.13!

joshgachnang commented 7 months ago

@francescov1 you are amazing, thank you! I'll try this out tomorrow!