[英]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-service
( https://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:更多细节:
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}')
这样您就可以查看两者而无需更改src
和dist
Note: join
is from the path
module注意:
join
来自path
模块
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.