francescov1 / mongoose-tsgen

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

Parser failure for imports with .js extension #160

Closed orgads closed 2 months ago

orgads commented 3 months ago

enums.ts:

export enum MyEnum {
  VAL1 = 'Val1',
  VAL2 = 'Val2'
}

schema.ts:

import { MyEnum } from './enums.js';

Error:

Generating mongoose typescript definitions... !
    Error: Cannot find module './enums.js'
    Require stack:
    - /project/schema.ts
    - /project/node_modules/mongoose-tsgen/lib/parser/utils.js
    - /project/node_modules/mongoose-tsgen/lib/parser/schema.js
    - /project/node_modules/mongoose-tsgen/lib/helpers/generator.js
    - /project/node_modules/mongoose-tsgen/lib/index.js
    - /project/node_modules/mongoose-tsgen/bin/run
    Code: MODULE_NOT_FOUND
francescov1 commented 3 months ago

@orgads You're trying to import a TS file as if its a JS file. Changing import { MyEnum } from './enums.js'; to import { MyEnum } from './enums.ts'; or import { MyEnum } from './enums'; should fix it

orgads commented 3 months ago

This is how imports are done in esm. This project is cjs, but the imports are esm compatible.

Changing it to .ts won't work.

francescov1 commented 3 months ago

Enums are only supported in Typescript, so the enum file would have to be enum.ts. I dont think you can import a ts file using a js extension can you?

orgads commented 3 months ago

Yes you can, See here.

And enums are transpiled to:

var MyEnum;
(function (MyEnum) {
    MyEnum["VAL1"] = "Val1";
    MyEnum["VAL2"] = "Val2";
})(MyEnum || (exports.MyEnum = MyEnum = {}));
francescov1 commented 3 months ago

Got it, thanks for clearing up, havent seen it before. Any chance you can provide a simple reproduction repo and ill add support for it?

orgads commented 3 months ago

https://github.com/orgads/js-import

francescov1 commented 2 months ago

Thanks @orgads will look this weekend

francescov1 commented 2 months ago

Fixed in v9.4.2 let me know if you have any more issues