镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Cannot import decorators-legacy in V7 #552

Open

Description

@w0rldart

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_modules seems to have all the requested data
image

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 deduped
  • tsconfig.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.

Activity

  1. ephys commented on Jul 25, 2023

    @ephys
    Member

    You need to set your tsconfig moduleResolution option to node16 or nodenext: https://www.typescriptlang.org/docs/handbook/esm-node.html#packagejson-exports-imports-and-self-referencing

  2. w0rldart commented on Jul 26, 2023

    @w0rldart
    Author

    Thank you ephys for the reply. I will try it, but it doesn't make sense.
    Your own tsconfig has moduleResolution set just to node, like I do

    image

    If this turns out to be the issue, may I suggest that this is clearly reflected somewhere in the documentation?

  3. transferred this issue fromsequelize/sequelizeon Aug 15, 2023
  4. ephys commented on Aug 15, 2023

    @ephys
    Member

    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

  5. w0rldart commented on Aug 15, 2023

    @w0rldart
    Author

    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

    It would be handy to have an example tsconfig.json that's suitable to use in a fresh V7 project

  6. Lordfirespeed commented on Sep 15, 2023

    @Lordfirespeed

    This is pretty awful, I can't lie. NodeNext is practically unusable due to

    • lack of (good) documentation/examples
    • Impenetrable path aliasing (I vehemently reject ../../, that is an abomination)

    The exports block is a lovely QoL feature when you're on NodeNext, but NodeNext being required to use v7 at all? 😩

  7. ephys commented on Sep 15, 2023

    @ephys
    Member

    You can have path aliasing in your own project by configuring exports in your own package.json

    The 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 use resolvePackageJsonExports instead

  8. Lordfirespeed commented on Sep 15, 2023

    @Lordfirespeed

    yeah, sorry. Was very frustrated last night. Turned out my path aliasing was working just fine but needed the .js extensions - the error was incredibly vague ('cannot resolve...')

  9. WikiRik commented on Sep 15, 2023

    @WikiRik
    Member

    We have opened #583 to document this. Are we missing anything?

  10. ephys commented on Dec 13, 2023

    @ephys
    Member

    As mentioned in sequelize/sequelize#16866, we also need to update the SSCCE repo's tsconfig

  11. johneatmon commented on Dec 13, 2023

    @johneatmon

    FWIW, you can set moduleResolution to "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
      }
    }
  12. yharishy commented on Apr 4, 2025

    @yharishy

    By the given tsconfig.json, getting below error

    TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions