francescov1 / mongoose-tsgen

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

Relative imports #134

Closed ttingeyqfym closed 5 months ago

ttingeyqfym commented 7 months ago

I'm trying to configure mtgen so that my models can use relative imports. IE import { obj } from 'src/enums/obj' and I can't seem to figure out how to configure mtgen to allow it. I have a tsconfig.json set with the rootDir and is working for all other imports, just breaks when mtgen runs.

francescov1 commented 7 months ago

Hi @ttingeyqfym, for using relative imports, mongoose-tsgen looks for the paths field to be set in your tsconfig.json file. Are you able to use this option? If not, I'd be happy to look at supporting rootDir if you can provide me a simple repo that reproduces your setup.

francescov1 commented 6 months ago

@ttingeyqfym Have you had a chance to try the paths field mentioned above?

samislam commented 5 months ago

@francescov1

Not sure if this is the issue related to my question, but in my models files, i do imports like this at the top of my file:

import { Site } from '@/types'

I have this tsconfig:

{
  "compilerOptions": {
    "target": "ES2022",
    // "lib": [],
    // "experimentalDecorators": true,
    // "emitDecoratorMetadata": true,
    "module": "ESNext",
    "moduleResolution": "Bundler",
    "baseUrl": "./src", // Set the base directory for relative paths
    "paths": {
      "@/*": ["*"] // Map @/* to */ (any file in the base directory)
    },
    // "moduleResolution": "node10",
    "outDir": "./dist",
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "skipLibCheck": true
  }
}

and I keep getting a syntax error here:

$ npx mtgen -d ./src/lib/models/ --debug                  
Debug mode enabled
Generating mongoose typescript definitions... !
    SyntaxError: Cannot use import statement outside a module
zsh: exit 1     npx mtgen -d ./src/lib/models/ --debug
francescov1 commented 5 months ago

@samislam I think this is due to your package.json missing the line "type": "module".

If you're still running into issues, itd be great if you can provide a simple repo that reproduces the issue and Id be happy to investigate.

samislam commented 5 months ago

@samislam I think this is due to your package.json missing the line "type": "module".

If you're still running into issues, itd be great if you can provide a simple repo that reproduces the issue and Id be happy to investigate.

I have type module.

francescov1 commented 5 months ago

I see. Could you create a simple repo to reproduce the issue and I'll investigate? Often these issues are related to the combo of packages being used so hard to debug without seeing the entire setup.

francescov1 commented 5 months ago

@samislam Found the issue, its related to ESNext. Fix is now live in v9.2.13!