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.

Error: Cannot find module (typeorm entity)

See original GitHub issue

When I do seed:run in my NestJS app I get the following error:

�  TypeORM Seeding v1.6.1
- Loading ormconfig
✔ ORM Config loaded
- Import Factories
✖ Could not import factories!
Error: Cannot find module 'src/users/user.entity'
Require stack:
- C:\Users\User\Development\example\example-api\src\database\factories\user.factory.ts
- C:\Users\User\Development\example\example-api\node_modules\typeorm-seeding\dist\utils\file.util.js
- C:\Users\User\Development\example\example-api\node_modules\typeorm-seeding\dist\commands\seed.command.js
- C:\Users\User\Development\example\example-api\node_modules\typeorm-seeding\dist\cli.js
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:797:15)
    at Function.Module._load (internal/modules/cjs/loader.js:690:27)
    at Module.require (internal/modules/cjs/loader.js:852:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at Object.<anonymous> (C:\Users\User\Development\example\example-api\src\database\factories\user.factory.ts:3:1)
    at Module._compile (internal/modules/cjs/loader.js:959:30)
    at Module.m._compile (C:\Users\User\Development\example\example-api\node_modules\ts-node\src\index.ts:858:23)
    at Module._extensions..js (internal/modules/cjs/loader.js:995:10)
    at Object.require.extensions.<computed> [as .ts] (C:\Users\User\Development\example\example-api\node_modules\ts-node\src\index.ts:861:12)
    at Module.load (internal/modules/cjs/loader.js:815:32) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    'C:\\Users\\User\\Development\\example\\example-api\\src\\database\\factories\\user.factory.ts',
    'C:\\Users\\User\\Development\\example\\example-api\\node_modules\\typeorm-seeding\\dist\\utils\\file.util.js',
    'C:\\Users\\User\\Development\\example\\example-api\\node_modules\\typeorm-seeding\\dist\\commands\\seed.command.js',
    'C:\\Users\\User\\Development\\example\\example-api\\node_modules\\typeorm-seeding\\dist\\cli.js'
  ]
}

This is my seed:config:

�  TypeORM Seeding v1.6.1
{
  type: 'postgres',
  host: 'localhost',
  port: 5432,
  username: 'example',
  password: 'example',
  database: 'example',
  entities: [ 'dist/**/*.entity{.ts,.js}' ],
  migrations: [ 'migration/*.js' ],
  cli: { migrationsDir: 'migration' },
  synchronize: true,
  factories: [ 'src/database/factories/**/*{.ts,.js}' ],
  seeds: [ 'src/database/seeds/**/*{.ts,.js}' ]
}

TypeORM is working fine with my app, but not this seeding package.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

16reactions
ponahoumcommented, Jul 15, 2021

Would recommend to use npm install --save-dev tsconfig-paths and in the seeding commands add -r tsconfig-paths/register, ie:

In the package.json

"fixtures:run:seed": "ts-node -r tsconfig-paths/register ./node_modules/typeorm-seeding/dist/cli.js seed -n src/fixtures/ormFixturesConfig.ts"

My config:

export default {
  type: 'mysql',
  url: process.env.DB_URL,
  logging: true,
  entities: ['src/**/*.entity.ts'],
  seeds: ['src/fixtures/seeds/**/*.seed.ts'],
  factories: ['src/fixtures/factories/**/*.factory.ts'],
};

It solved my issue. I think using .js files is a terrible idea, as often the dist folder content is not immediatly updated and the files may accumulate in it if you update the file names, causing strange effects when launching the fixtures.

15reactions
eugenio165commented, Jul 12, 2020

Hi xcymax, i’ve had this problem in the past. This happens because you’re using absolute paths. You should use relative paths in all your imports, so that when you run your code in prod mode, which would normally be something like node dist/main it all works correctly.

Also, in my experience i could not get my typeorm configuration to work with both .ts and .js files. If the above solution doesn’t work, i recommend you use your configuration with only .js files and use nest cli’s start command to start your project. This way, nest builds and executes your application straight with javascript, using the dist folder.

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 >
typeorm - npm
TypeORM is an ORM that can run in NodeJS, Browser, Cordova, PhoneGap, Ionic, React Native, NativeScript, Expo, and Electron platforms and ...
Read more >
cannot find module 'process' or its corresponding type ...
ts:1:92 - error TS2307: Cannot find module 'typeorm' or its corresponding type declarations. 1 import { Column, Entity, PrimaryGeneratedColumn, CreateDateColumn ...
Read more >
TypeORM Cannot use import statement outside a module
Hi the error is on your ormconfig.json file. check where is your code searching for the entities, migrations, subscribers in a dev, test ......
Read more >
typeorm/typeorm - Gitter
... data to the type of that entity, I get a type error that property: id is missing ... (function (exports, require, module,...
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 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