MikroORM cannot find module using absolute paths for importing

60 Views Asked by At

I am using NestJS with TypeScript and MikroORM with the MikroOrm-configuration

{
    ...
    entities: ['dist/db/entities'],
    entitiesTs: ['src/db/entities'],
    baseDir: process.cwd() // default value
    ...
}

and the folder structure

Project/
├─ backend/
│  ├─ dist/
│  │  ├─ (same structure as src)
│  ├─ src/
│  │  ├─ db/
│  │  │  ├─ entities/
│  │  │  │  ├─ File.ts
│  │  │  │  ├─ Directory.ts
|  ├─ mikro-orm.config.ts 
│  ├─ tsconfig.json 
|  ├─ package.json

tsconfig.json:

{
    "compilerOptions": {
        "module": "commonjs",
        "declaration": true,
        "removeComments": true,
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "allowSyntheticDefaultImports": true,
        "target": "ES2021",
        "sourceMap": true,
        "outDir": "./dist",
        "baseUrl": "./",
        "incremental": true,
        "skipLibCheck": true,
        "strictNullChecks": true,
        "noImplicitAny": true,
        "strictBindCallApply": true,
        "forceConsistentCasingInFileNames": true,
        "noFallthroughCasesInSwitch": true,
        "strict": true,
        "noUncheckedIndexedAccess": true,
        "exactOptionalPropertyTypes": true
    }
}

package.json:

{
    "dependencies": {
        "@mikro-orm/cli": "^6.0.4",
        "@mikro-orm/core": "^6.0.4",
        "@mikro-orm/mariadb": "^6.0.4",
        "@mikro-orm/migrations": "^6.0.4",
        "@mikro-orm/nestjs": "^5.2.3",
        "@nestjs/common": "^10.0.0",
        "@nestjs/config": "^3.1.1",
        "@nestjs/core": "^10.0.0",
        "@nestjs/platform-express": "^10.0.0",
        "@types/archiver": "^6.0.2",
        "archiver": "^6.0.1",
        "class-transformer": "^0.5.1",
        "class-validator": "^0.14.0",
        "dotenv": "^16.3.1",
        "mysql2": "^3.6.5",
        "reflect-metadata": "^0.1.13",
        "uuid": "^9.0.1"
    },
    "devDependencies": {
        "@nestjs/cli": "^10.0.0",
        "@nestjs/schematics": "^10.0.0",
        "@nestjs/testing": "^10.0.0",
        "@types/express": "^4.17.17",
        "@types/jest": "^29.5.2",
        "@types/k6": "^0.46.3",
        "@types/mime-types": "^2.1.1",
        "@types/multer": "^1.4.7",
        "@types/node": "^20.3.1",
        "@types/supertest": "^2.0.12",
        "@types/uuid": "^9.0.6",
        "@typescript-eslint/eslint-plugin": "^6.0.0",
        "@typescript-eslint/parser": "^6.0.0",
        "eslint": "^8.42.0",
        "eslint-config-prettier": "^9.0.0",
        "eslint-plugin-prettier": "^5.0.0",
        "jest": "^29.5.0",
        "k6": "^0.0.0",
        "prettier": "^3.0.0",
        "source-map-support": "^0.5.21",
        "supertest": "^6.3.3",
        "ts-jest": "^29.1.0",
        "ts-loader": "^9.4.3",
        "ts-node": "^10.9.2",
        "ts-node-dev": "^2.0.0",
        "tsconfig-paths": "^4.2.0",
        "typescript": "^5.1.3"
    },
    "mikro-orm": {
        "useTsNode": true
    }
}

Trying to import src/db/entities/Directory in File.ts throws an error when trying to create a migration usign npx mikro-orm migration:create and complains it cannot find module src/db/entities/Directory. Of course, relative paths would work, but is there any way to use absolute paths as well?

0

There are 0 best solutions below