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.

Cannot load seeds when `__dirname` is in path

See original GitHub issue

As seen on the TypeOrm documentation, one can use __dirname for the path to entities and stuff.

loadFiles util function, probably due to the use of process.cwd(), doesn’t load any seeds if the path is declared as such:

{
  // ormconfig.js
  seeds: [`${__dirname}/seeds/*.js`],
  ...
}

Any input on that ?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
bpatramcommented, Mar 30, 2020

I’m also affected by this. When loading the seed and factories an assumption is made that the path is relative and never absolute.

https://github.com/w3tecch/typeorm-seeding/blob/9b614d4c74871204e30ec69f298ccb012e5ed108/src/utils/file.util.ts#L6-L10 as the OP noticed… that join with process.cwd() on line 8

If we dig into TypeORM’s implementation in how they resolve files (like entities, migrations, etc) you’ll notice they do a simple check to see if the path is absolute (ie, the path has a leading slash), and if it is then they don’t prepend to it.

https://github.com/typeorm/typeorm/blob/442bff950704443e0b42fd0f950180d062f8389f/src/connection/ConnectionOptionsReader.ts#L145-L153

TypeORM seeding should most likely also follow this convention. Another potential issue I see is that this library prepends relative paths with the current working directory (cwd) rather than from the package root directory (or the directory containing the ormconfig file). This will most likely burn people using yarn workspaces or lerna or those running typeorm-seeding from a different directory (ie, npm run --prefix different/path ...)

1reaction
hirsch88commented, Apr 10, 2020

hi @bpatram thanks for the tip. 😃

I now use the ConnectionOptionReader of TypeORM. Check out the new version 1.4.0

Read more comments on GitHub >

github_iconTop Results From Across the Web

javascript - __dirname is not working in node js with ...
when i am bundling it with webpack and run the bundle file then this happens. Otherwise if i just run the normal app.js...
Read more >
Seeding a database | Zell Liew
The process where you add initial content to a database is called seeding. Here, I teach you how to seed a database automatically....
Read more >
Moving data in ML pipelines - Azure Machine Learning
To pass the dataset's path to your script, use the Dataset object's ... PRNG seed smaller_dataset = iris_dataset.take_sample(0.1, seed=seed) ...
Read more >
Migrations | Sequelize
To manage all data migrations you can use seeders. Seed files are some change in data that can be used to populate database...
Read more >
path.normalize(p) : nodejs API
dirname (p) #. Return the directory name of a path. Similar to the Unix dirname command. Example: ?
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