question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Running npm run makemigrations throws 'Cannot find module '@entity''

See original GitHub issue

Version of FoalTS: 2.6.0

When I try to run the npm run makemigrations command, if I have some imports in my entities that use the @entity it throws this error:

Error during migration generation: Error: Cannot find module ‘@entity

This is the makemigrations command (should be the default one) : foal rmdir build && tsc -p tsconfig.app.json && npx typeorm migration:generate --name migration && tsc -p tsconfig.app.json

and this is my tsconfig.app.json :

{
    "extends": "./tsconfig.json",
    "include": [
        "src/**/*.ts"
    ],
    "exclude": [
        "src/e2e/*.ts",
        "src/**/*.spec.ts",
        "src/e2e.ts",
        "src/test.ts"
    ]
}

tsconfig.json:

{
    "compilerOptions": {
        "module": "commonjs",
        "sourceMap": true,                              /* Generates corresponding '.map' file. */
        "outDir": "build",                              /* Redirect output structure to the directory. */
        "strict": true,                                 /* Enable all strict type-checking options. */
        "strictPropertyInitialization": false,          /* Enable strict checking of property initialization in classes. */
        "moduleResolution": "node",                     /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
        "esModuleInterop": true,                        /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
        "experimentalDecorators": true,                 /* Enables experimental support for ES7 decorators. */
        "emitDecoratorMetadata": true,                  /* Enables experimental support for emitting type metadata for decorators. */
        "skipLibCheck": true,                           /* Skip type checking of declaration files. */
        "forceConsistentCasingInFileNames": true,       /* Disallow inconsistently-cased references to the same file. */
        "noImplicitAny": false,
        "target": "ES2021",
        "resolveJsonModule": true,
        "rootDir": "src",
        "lib": [
            "ES2021"
        ],
        "baseUrl": "src",
        "paths": {
            "@lib": ["lib"],
            "@entity": ["app/entities"],
            "@service": ["app/services"],
            "@interface": ["app/interfaces"],
            "@hook": ["app/hooks"],
            "@middleware": ["app/middleware"]
        }
    },
    "include": [
        "src/**/*.ts",
        "ormconfig.js",
        "config/*.js"
    ]
}

and package.json :

    "_moduleAliases": {
        "@service": "build/app/services",
        "@entity": "build/app/entities",
        "@interface": "build/app/interfaces",
        "@hook": "build/app/hooks",
        "@middleware": "build/app/middleware",
        "@lib": "build/lib",
        "@dbconfig": "build/ormconfig.js"
    },

It works fine if I import using from './' but I want to know why it’s not working with import { ... } from '@entity'

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
LoicPoullaincommented, Jan 18, 2022

Ok, so the problem probably comes from here.

When running the application, the index.ts file is read at first and so the module-alias module is registered.

But in the case of the migrations, this file is not read. Only the migration files and the files they import are. This is probably why you get this error.

Could the commands npx typeorm -r module-alias/register migration:run or node -r module-alias/register ./node_modules/.bin/typeorm migration:run fix the problem? Or something similar?

0reactions
LoicPoullaincommented, Sep 30, 2022

I tried and it didn’t work. What seems to work is when I import the module-alias inside the module index.ts (so in entity/index.ts). Maybe would be good to mention that in the docs

Glad that you found how to fix the issue! I’d rather not to add it to the documentation as I feel like it is a specific issue related to the use of a 3rd-part library (module-alias). If anyone has the same problem, he or she will be able to find the solution on this issue. 🙂

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeOrm migration - Error: Cannot find module - Stack Overflow
Now I'm getting the error: RepositoryNotFoundError: No repository for "permissions" was found. Looks like this entity is not registered in ...
Read more >
cannot find module [Node npm Error Solved] - freeCodeCamp
To fix the error, you need to install the package that is absent in your project directory – npm install package-name or yarn...
Read more >
cannot find module 'path' or its corresponding type declarations
To solve the "Cannot find module `path` or its corresponding type declarations" error, install the types for node by running the command `npm...
Read more >
Building a REST API with NestJS and Prisma
With the Prisma schema defined, you will run migrations to create the ... throwing an exception that says: Error: Cannot find module '....
Read more >
Migrations - typeorm - GitBook
import { Entity, Column, PrimaryGeneratedColumn } from "typeorm" ... This command will execute all pending migrations and run them in a sequence ordered...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found