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
While using Bun with sequelize-typescript, using paths for the models property fails to work when the relevant modules are ESM, throwing No default export defined for file "FILE" or export does not satisfy filename.
This is because Bun allows using require even if the target is an ES module:
In Bun's JavaScript runtime, require can be used by both ES Modules and CommonJS modules. If the target module is an ES Module, require returns the module namespace object (equivalent to import * as). If the target module is a CommonJS module, require returns the module.exports object (as in Node.js). Source
So what happens, is that the module is successfully imported through require , but when sequelize-typescript attempts to match over the attributes ( Object.keys(module)), it fails to find any.
$ bun run index.ts
45 | const module = require(fullPath);
46 | const fileName = (0, path_1.basename)(fullPath);
47 | const matchedMemberKey = Object.keys(module).find((m) => modelMatch(fileName, m));
48 | const matchedMember = matchedMemberKey ? module[matchedMemberKey] : undefined;
49 |if (!matchedMember &&!module.default) {
50 | throw new Error(`No default export defined for file "${fileName}" or ` +
^
error: No default export defined for file "Person" or export does not satisfy filename.
at node_modules/sequelize-typescript/dist/sequelize/sequelize/sequelize-service.js:50:27
at map (1:11)
at node_modules/sequelize-typescript/dist/sequelize/sequelize/sequelize-service.js:43:18
at reduce (1:11)
at addModels (node_modules/sequelize-typescript/dist/sequelize/sequelize/sequelize.js:36:48)
at new Sequelize (node_modules/sequelize-typescript/dist/sequelize/sequelize/sequelize.js:20:22)
at index.ts:4:1
Bun v1.1.27 (Linux x64)
What to do about it
Instead of using Object.keys, I believe we could just use for ... in, which would pick up the inherited enumerable attributes:
This works under Bun. That said, per #1660, I imagine this isn't a supported setup, so I mostly wanted to create this issue to prevent others from wasting several hours of their day trying to figure out what's wrong. If you're in this situation, you can get around this by importing the models directly and passing them to models:
Versions
Issue type
The problem
While using Bun with
sequelize-typescript
, using paths for themodels
property fails to work when the relevant modules are ESM, throwingNo default export defined for file "FILE" or export does not satisfy filename
.This is because Bun allows using
require
even if the target is an ES module:So what happens, is that the module is successfully imported through
require
, but whensequelize-typescript
attempts to match over the attributes (Object.keys(module)
), it fails to find any.sequelize-typescript/src/sequelize/sequelize/sequelize-service.ts
Lines 51 to 62 in 8ded61a
Per spec, exports of ES modules aren't enumerable attributes, so
Object.keys(module)
will return[]
regardless of whether the attributes exist or not.Steps to reproduce
index.ts
models/Person.ts
:What to do about it
Instead of using
Object.keys
, I believe we could just usefor ... in
, which would pick up the inherited enumerable attributes:This works under Bun. That said, per #1660, I imagine this isn't a supported setup, so I mostly wanted to create this issue to prevent others from wasting several hours of their day trying to figure out what's wrong. If you're in this situation, you can get around this by importing the models directly and passing them to
models
:The text was updated successfully, but these errors were encountered: