简体   繁体   English

sequelize-typescript - 使用带有 `tsc` 编译器(带有 NestJS)的 glob 配置时无法解析模型模块

[英]sequelize-typescript - When using glob configuration with `tsc` compiler (with NestJS) cannot resolve model modules

I am setting up sequelize-typescript and then adding models:我正在设置sequelize-typescript然后添加模型:

    import appRoot from 'app-root-path'

    connection = new Sequelize({... connection details})
    const appGlobPattern = join(appRoot.toString(), 'src/**/entities/*.entity.ts')
    // appGlobPattern = absolute path to the root of the project (i.e. C:/Users/user/my-project/)
    this.connection.addModels([appGlobPattern])

An error is thrown when trying to do the require(fullPath) line in sequelize-service ( https://github.com/RobinBuschmann/sequelize-typescript/blob/1c34e87c968b1c8333ef6467c2dd44e069e06fa8/src/sequelize/sequelize/sequelize-service.ts#L57 )试图做的时候,则会引发错误require(fullPath)符合sequelize-servicehttps://github.com/RobinBuschmann/sequelize-typescript/blob/1c34e87c968b1c8333ef6467c2dd44e069e06fa8/src/sequelize/sequelize/sequelize-service.ts#L57 )

An error is thrown as the file resolver cannot resolve the file (which exists):由于文件解析器无法解析文件(存在),因此会引发错误:

Error: Cannot find module 'C:\\Users\\user\\my-project\\src\\entities\\my-model.entity'错误:找不到模块“C:\\Users\\user\\my-project\\src\\entities\\my-model.entity”

Full stack track全栈轨道

Error: Cannot find module 'C:\Users\........\notification\entities\communication-channel.entity'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)
    at Function.Module._load (internal/modules/cjs/loader.js:562:25)
    at Module.require (internal/modules/cjs/loader.js:692:17)
    at require (internal/modules/cjs/helpers.js:25:18)
    at glob.sync.filter.map.filter.map.fullPath (C:\Users\........\notification\node_modules\sequelize-typescript\dist\sequelize\sequelize\sequelize-service.js:44:32)
    at Array.map (<anonymous>)
    at arg.reduce (C:\Users\........\notification\node_modules\sequelize-typescript\dist\sequelize\sequelize\sequelize-service.js:43:18)
    at Array.reduce (<anonymous>)
    at Object.getModels (C:\Users\........\notification\node_modules\sequelize-typescript\dist\sequelize\sequelize\sequelize-service.js:35:20)
    at Sequelize.addModels (C:\Users\........\notification\node_modules\sequelize-typescript\dist\sequelize\sequelize\sequelize.js:34:44)
    at DatabaseConnection._addModelsToConnection (C:\Users\........\notificationn\dist\database\classes\database-connection.js:33:25)
    at DatabaseConnection.createConnection (C:\Users\........\notification\dist\database\classes\database-connection.js:23:14)
    at InstanceWrapper.useFactory [as metatype] (C:\Users\........\notification\dist\database\providers\database-connection-pool.provider.js:13:30)
    at process._tickCallback (internal/process/next_tick.js:68:7)
    at Function.Module.runMain (internal/modules/cjs/loader.js:834:11)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)

It seems to potentially be something with the tsc compiler that NestJS is using by default.它似乎可能与 NestJS 默认使用的 tsc 编译器有关。 Or perhaps module loader does not play nice with absolute file paths.或者也许模块加载器在绝对文件路径上表现不佳。


More details:更多细节:

  • sequelize: "version": "5.21.2"续集:“版本”:“5.21.2”
  • sequelize-typescript: "version": "1.0.0"续集打字稿:“版本”:“1.0.0”
  • typescript "version": "3.7.2"打字稿“版本”:“3.7.2”

These are my tsconfig settings:这些是我的 tsconfig 设置:

{
  "compilerOptions": {
    "module": "CommonJS",
    "moduleResolution": "node",
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "esModuleInterop": true,
    "target": "ES6",
    "sourceMap": true,
    "outDir": "./dist",
    "baseUrl": "./",
    "incremental": true,
    "paths": {
      "express": ["src/multi-tenancy/types/*"]
    }
  },
  "exclude": ["node_modules", "dist"]
}

You shouldn't be using src as part of your file path, as once you compile the code from Typescript to JavaScript, your code now lives in dist and the src folder is lost in the context.您不应该将src用作文件路径的一部分,因为一旦将代码从 Typescript 编译为 JavaScript,您的代码现在位于dist ,而src文件夹将丢失在上下文中。 You should also change the .ts to .js as that's what files will be resolved at runtime.您还应该将.ts更改为.js因为这是在运行时解析的文件。 The other option is to use a glob like join(__dirname, '{dist,src}/**/entities/*.entity.{js,ts}') so that you can look at both without changing your code between src and dist .另一种选择是使用像join(__dirname, '{dist,src}/**/entities/*.entity.{js,ts}')这样的glob join(__dirname, '{dist,src}/**/entities/*.entity.{js,ts}')这样您就可以查看两者而无需更改srcdist

Note: join is from the path module注意: join来自path模块

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 sequelize-typescript:模型中get()的通用方法 - sequelize-typescript: Common Method for get() in a model Sequelize-typescript 'HasManyCreateAssociationMixin' 不是 function - Sequelize-typescript 'HasManyCreateAssociationMixin' is not a function 使用sequelize-typescript连接到PostgreSQL的Angular 6 Webapp - Angular 6 webapp that connects to PostgreSQL using sequelize-typescript 如何使用sequelize-typescript访问sequelize实例 - How to access sequelize instance with sequelize-typescript Sequelize beforeConnect 钩子与 sequelize-typescript 未运行 - Sequelize beforeConnect hook with sequelize-typescript not running 使用 tsc 使用 esm 编译 TypeScript 时出现“找不到模块” - “Cannot find module” when using tsc to compile TypeScript using esm 是否可以在不使用 sequelize-typescript 框架的情况下在 NodeJS 中使用 Sequelize typescript? - Is it possible to use Sequelize typescript in NodeJS without using sequelize-typescript framework? 在我定义 model 之后,我需要 Model.init() 吗? 续集打字稿 - Do i need to Model.init() after i define the model? sequelize-typescript 编译错误 sequelize-typescript 但适用于 nodemon - Compiling error sequelize-typescript but works with nodemon 即使 tsc 成功解决了它,也找不到 Typescript 模块 - Cannot find Typescript module even though tsc successfully manages to resolve it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM