Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot import decorators-legacy in V7 #552

Open
3 of 6 tasks
w0rldart opened this issue Jul 24, 2023 · 10 comments
Open
3 of 6 tasks

Cannot import decorators-legacy in V7 #552

w0rldart opened this issue Jul 24, 2023 · 10 comments

Comments

@w0rldart
Copy link

w0rldart commented Jul 24, 2023

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.

@ephys
Copy link
Member

ephys commented Jul 25, 2023

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

@w0rldart
Copy link
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?

@ephys ephys transferred this issue from sequelize/sequelize Aug 15, 2023
@ephys
Copy link
Member

ephys commented 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
Copy link
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

@Lordfirespeed
Copy link

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? 😩

@ephys
Copy link
Member

ephys commented Sep 15, 2023

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

@Lordfirespeed
Copy link

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...')

@WikiRik
Copy link
Member

WikiRik commented Sep 15, 2023

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

@ephys
Copy link
Member

ephys commented Dec 13, 2023

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

@johneatmon
Copy link

johneatmon commented Dec 13, 2023

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
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants