website icon indicating copy to clipboard operation
website copied to clipboard

Cannot import decorators-legacy in V7

Open w0rldart opened this issue 2 years ago • 10 comments

Issue Creation Checklist

  • [x] I understand that my issue will be automatically closed if I don't fill in the requested information
  • [x] 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
[email protected] 
└── @sequelize/[email protected]
  • Node.js version:
$ node -v
v18.12.1
  • If TypeScript related: TypeScript version:
npm list typescript
[email protected]
├─┬ [email protected]
│ └── [email protected]
└─┬ [email protected]
  ├─┬ @typescript-eslint/[email protected]
  │ └─┬ [email protected]
  │   └── [email protected] deduped
  ├─┬ [email protected]
  │ └── [email protected] deduped
  └── [email protected] 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"],
  },
}

Would you be willing to resolve this issue by submitting a Pull Request?

  • [ ] Yes, I have the time and I know how to start.
  • [x] 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.

w0rldart avatar Jul 24 '23 19:07 w0rldart

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

ephys avatar Jul 25 '23 06:07 ephys

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?

w0rldart avatar Jul 26 '23 10:07 w0rldart

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

ephys avatar Aug 15 '23 08:08 ephys

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

w0rldart avatar Aug 15 '23 19:08 w0rldart

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? :weary:

Lordfirespeed avatar Sep 15 '23 02:09 Lordfirespeed

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

ephys avatar Sep 15 '23 06:09 ephys

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

Lordfirespeed avatar Sep 15 '23 13:09 Lordfirespeed

We have opened https://github.com/sequelize/website/pull/583 to document this. Are we missing anything?

WikiRik avatar Sep 15 '23 13:09 WikiRik

As mentioned in https://github.com/sequelize/sequelize/issues/16866, we also need to update the SSCCE repo's tsconfig

ephys avatar Dec 13 '23 12:12 ephys

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

johneatmon avatar Dec 13 '23 16:12 johneatmon