You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm having trouble getting my models to be imported correctly by sequelize-typescript. The error is:
Error [ERR_REQUIRE_ESM]: require() of ES Module <root>/packages/api/dist/models/my_model.js from <root>/node_modules/sequelize-typescript/dist/sequelize/sequelize/sequelize-service.js not supported.
Instead change the require of access_method.js in <root>/node_modules/sequelize-typescript/dist/sequelize/sequelize/sequelize-service.js to a dynamic import() which is available in all CommonJS modules.
at <root>/node_modules/sequelize-typescript/dist/sequelize/sequelize/sequelize-service.js:45:32
at Array.map (<anonymous>)
at <root>/node_modules/sequelize-typescript/dist/sequelize/sequelize/sequelize-service.js:43:18
at Array.reduce (<anonymous>)
at getModels (<root>/node_modules/sequelize-typescript/dist/sequelize/sequelize/sequelize-service.js:35:20)
at Sequelize.addModels (<root>/node_modules/sequelize-typescript/dist/sequelize/sequelize/sequelize.js:36:58)
at new Sequelize (<root>/node_modules/sequelize-typescript/dist/sequelize/sequelize/sequelize.js:20:22)
at file://<root>/packages/api/dist/db.js:12:26
at async Promise.all (index 0) {
code: 'ERR_REQUIRE_ESM'
}
I can see in the sequelize-typescript source that it's trying to require() my models (as opposed to a dynamic import).
Versions
sequelize: 6.30.0
sequelize-typescript: 2.1.5
typescript: 5.0.3
node: 16.15.1
Issue type
bug report
feature request
Actual behavior
After compiling with: npx tsc -p tsconfig.json
And running with: node --experimental-specifier-resolution=node dist/index.js
I'll get the above error.
Expected behavior
No error
Related code
I'm using the models config option to load models like so:
exportconstsequelize=newSequelize(svcConfig.databaseURL,{// other configmodels: [path.join(__dirname,'/models')],});
Thanks @loss-and-quick. I do have that set. In the end, I think the issue is similar to what this PR is attempting to solve: #1206
I have a few cyclical dependencies arising from, for example, ModelA importing ModelB for a belongsTo declaration, and ModelB hasMany ModelA.
Using models: [path.join(__dirname, '/models')] config, the loader gets stuck when trying to import Model A, which transitively imports B, which will wait until A resolves (it never does, as it's still waiting for B).
I worked around this by importing all the DB models directly in my sequelize config/setup file and passing the Model imports directly to the models option like so: models: [ModelA, ModelB, ModelC]
It's not ideal (as I like the idea of sequelize auto-discovering, importing new models, and not having to explicitly declare them all in a long list), but it does work around using sequelize's module loader code which seems to get stuck on dynamic imports.
Issue
I'm having trouble getting my models to be imported correctly by sequelize-typescript. The error is:
I can see in the sequelize-typescript source that it's trying to require() my models (as opposed to a dynamic import).
Versions
Issue type
Actual behavior
After compiling with:
npx tsc -p tsconfig.json
And running with:
node --experimental-specifier-resolution=node dist/index.js
I'll get the above error.
Expected behavior
No error
Related code
I'm using the
models
config option to load models like so:My models are in the format of
My models get generated (by tsc) like so:
My tsconfig is:
The text was updated successfully, but these errors were encountered: