我有一个 typescript 模块 @tlabs/models我只是在index.ts中导出:
export * from '../models......'
在每个文件中我都有类似的内容:
export const Project = typedModel('projects', ProjectSchema);
我唯一的依赖项是在每个文件中导入的 ts-mongoose
,如下所示:
import { createSchema, Type, typedModel, ExtractProps } from 'ts-mongoose';
ts-mongoose 是一个依赖项,它本身需要 mongoose + mongoose 类型。
在我的 typescript Node 项目中,我有 ts-mongoose
、mongoose
和 @tlabs/models 作为依赖项,@types/mongoose
作为开发依赖性。
运行 tsc 没问题,文件被编译并且没有错误被抛出,但尝试运行实际文件会抛出:
Error: Cannot find module '@tlabs/models'
我已经重新安装了所有模块几次,并通过 vscode 检查了 package.json 以及磁盘上的实际文件,它们就在那里。
我错过了什么?
我的 tsconfig 是:
{
"include": ["src/**/*"],
"exclude": ["node_modules", "./node_modules", "./node_modules/*"],
"compilerOptions": {
"module": "commonjs",
"esModuleInterop": true,
"target": "es6",
"allowJs": true,
"moduleResolution": "node",
"sourceMap": true,
"outDir": "dist",
"noImplicitAny": false,
"alwaysStrict": true,
"strictNullChecks": true,
"types": [],
"lib": [],
"experimentalDecorators": true
}
}
在我的 Node 项目中使用/导入它的正确方法是什么?
最佳答案
我的最终 TS 配置:
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"declaration": true,
"declarationMap": true,
"sourceMap": true,
"outDir": "lib",
"strict": true,
"esModuleInterop": true
},
"include": ["src"]
}
以及相关的package.json配置:
{
"main": "lib/index.js",
"types": "lib",
"scripts": {
"tsc": "tsc",
"build": "tsc -p ."
},
"files": [
"lib"
],
}
关于node.js - 在 Typescript Node+Express 项目中使用模块中的 TypeScript 文件的正确方法?当前抛出错误 : Cannot find module,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56776560/