Cannot load seeds when `__dirname` is in path
See original GitHub issueAs 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:
- Created 4 years ago
- Reactions:1
- Comments:5 (1 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

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 8If 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 ...)hi @bpatram thanks for the tip. 😃
I now use the ConnectionOptionReader of TypeORM. Check out the new version 1.4.0