Repository navigation
Cannot import decorators-legacy in V7 #552
Copy link
Copy link
Open
Description
Issue Creation Checklist
- I understand that my issue will be automatically closed if I don't fill in the requested information
- I have read the contribution guidelines
Bug Description
The documentation explicitly indicates to use decorators to define all aspects related to a model.
From types, to relationships, and more.
However, I personally cannot get it to work, due to this issue in my VSCode editor
Cannot find module '@sequelize/core/decorators-legacy' or its corresponding type declarations.ts(2307)
Reproducible Example
- I use TypeScript
- Everything works fine without importing or trying to use decorators. I.e.:
import { Model, DataTypes, InferAttributes, InferCreationAttributes, CreationOptional } from '@sequelize/core'
import { RequestType } from '../types'
import { sequelize } from '../db'
class Request extends Model<InferAttributes<Request>, InferCreationAttributes<Request>> {
declare id: CreationOptional<number>
declare requestType: string
declare requestData: object
declare requester: string
declare createdAt: CreationOptional<Date>
declare updatedAt: CreationOptional<Date>
}
Request.init({
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
requestType: {
type: DataTypes.ENUM(
RequestType.EXPENV_AZURE,
RequestType.EXPENV_GCP,
RequestType.VM_AZURE,
RequestType.VM_GCP,
RequestType.RPA_AZURE,
RequestType.RPA_GCP,
RequestType.AZURE_RG
),
allowNull: true
},
requestData: {
type: DataTypes.JSON,
allowNull: false
},
requester: {
type: DataTypes.STRING,
allowNull: false
},
createdAt: {
type: DataTypes.DATE
},
updatedAt: {
type: DataTypes.DATE
}
}, { sequelize })- The sequelize directory inside
node_modulesseems to have all the requested data
However, and as per a standard example on this page, the usual flow to use decorators
import { Model, DataTypes, InferAttributes, InferCreationAttributes, CreationOptional, NonAttribute } from '@sequelize/core'
import { PrimaryKey, Attribute, AutoIncrement, NotNull, HasMany, BelongsTo } from '@sequelize/core/decorators-legacy'has the second line, no matter what else I have in my file, always throw this error:
Cannot find module '@sequelize/core/decorators-legacy' or its corresponding type declarations.ts(2307)
What do you expect to happen?
Find the module and not throw that error
What is actually happening?
Environment
- Sequelize version:
$ npm list @sequelize/core
db-operator@1.0.0
└── @sequelize/core@7.0.0-alpha.27- Node.js version:
$ node -v
v18.12.1- If TypeScript related: TypeScript version:
npm list typescript
db-operator@1.0.0
├─┬ ts-jest@29.1.1
│ └── typescript@5.1.6
└─┬ ts-standard@12.0.2
├─┬ @typescript-eslint/eslint-plugin@5.62.0
│ └─┬ tsutils@3.21.0
│ └── typescript@5.1.6 deduped
├─┬ eslint-config-standard-with-typescript@23.0.0
│ └── typescript@5.1.6 deduped
└── typescript@5.1.6 dedupedtsconfig.json
{
"compilerOptions": {
"allowJs": false,
"declaration": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"module": "commonjs",
"moduleResolution": "node",
"outDir": "dist",
"rootDir": ".",
"skipLibCheck": true,
"sourceMap": true,
"strict": true,
"strictNullChecks": true,
"target": "es6",
"types": ["node"],
},
}
- Database & Version:
MySQL 8 - Connector library & Version:
mysql2@3.5.2
Would you be willing to resolve this issue by submitting a Pull Request?
- Yes, I have the time and I know how to start.
- Yes, I have the time but I will need guidance.
- No, I don't have the time, but my company or I are supporting Sequelize through donations on OpenCollective.
- No, I don't have the time, and I understand that I will need to wait until someone from the community or maintainers is interested in resolving my issue.
Indicate your interest in the resolution of this issue by adding the 👍 reaction. Comments such as "+1" will be removed.
Reacted by Rafael Rossi, Hans, Nathan Alder and MaudeClonet
Metadata
Metadata
Assignees
Labels
No labels
Activity
ephys commentedon Jul 25, 2023
You need to set your tsconfig
moduleResolutionoption tonode16ornodenext: https://www.typescriptlang.org/docs/handbook/esm-node.html#packagejson-exports-imports-and-self-referencingw0rldart commentedon Jul 26, 2023
Thank you ephys for the reply. I will try it, but it doesn't make sense.
Your own tsconfig has
moduleResolutionset just tonode, like I doIf this turns out to be the issue, may I suggest that this is clearly reflected somewhere in the documentation?
ephys commentedon Aug 15, 2023
Our own tsconfig should not be used as an example, it's overly complicated because we try to support multiple versions of TypeScript
I've converted this to a documentation issue
w0rldart commentedon Aug 15, 2023
It would be handy to have an example
tsconfig.jsonthat's suitable to use in a fresh V7 projectLordfirespeed commentedon Sep 15, 2023
This is pretty awful, I can't lie.
NodeNextis practically unusable due to../../, that is an abomination)The
exportsblock is a lovely QoL feature when you're on NodeNext, but NodeNext being required to use v7 at all? 😩ephys commentedon Sep 15, 2023
You can have path aliasing in your own project by configuring
exportsin your own package.jsonThe explanation for the different type of moduleResolutions is described here: https://www.typescriptlang.org/tsconfig#moduleResolution
If that is not sufficient, I would recommend opening an issue on the TypeScript repo about it instead
You don't technically have to use Node16/NodeNext/Bundler (though I don't see why you wouldn't use the ts module resolution that accurately replicates how node resolves modules if you're using node). You can configure
paths, or useresolvePackageJsonExportsinsteadLordfirespeed commentedon Sep 15, 2023
yeah, sorry. Was very frustrated last night. Turned out my path aliasing was working just fine but needed the
.jsextensions - the error was incredibly vague ('cannot resolve...')WikiRik commentedon Sep 15, 2023
We have opened #583 to document this. Are we missing anything?
ephys commentedon Dec 13, 2023
As mentioned in sequelize/sequelize#16866, we also need to update the SSCCE repo's tsconfig
johneatmon commentedon Dec 13, 2023
FWIW, you can set
moduleResolutionto "Bundler" and it also works. (TS notes that "This is the recommended setting in TypeScript 5.0+ for applications that use a bundler.")I have Sequelize v7 with decorators working in a Next.js 14 project, and I'm basically using the default create-t3-app
tsconfig.json. Posting this here in case anyone needs a similar resolution:{ "compilerOptions": { /* Base Options */ "esModuleInterop": true, "skipLibCheck": true, "target": "ES2022", "allowJs": true, "resolveJsonModule": true, "moduleDetection": "force", "isolatedModules": true, /* Sequelize */ "experimentalDecorators": true, "emitDecoratorMetadata": true, "allowImportingTsExtensions": true, /* Strictness */ "strict": true, "noUncheckedIndexedAccess": true, "forceConsistentCasingInFileNames": true, "checkJs": true, /* Bundled projects */ "lib": ["dom", "dom.iterable", "ES2022"], "noEmit": true, "module": "ESNext", "moduleResolution": "Bundler", "jsx": "preserve", "plugins": [{ "name": "next" }], "incremental": true, /* Path aliases */ "baseUrl": ".", "paths": { "@/*": ["./*"] }, /* Needed for ts-node */ "typeRoots": ["**/*.d.ts"] }, "include": [ ".eslintrc.cjs", "next-env.d.ts", "**/*.ts", "**/*.tsx", "**/*.cjs", "**/*.js", ".next/types/**/*.ts" ], "exclude": ["node_modules"], "ts-node": { "esm": true } }yharishy commentedon Apr 4, 2025
By the given tsconfig.json, getting below error
TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts"