我正在尝试使用OvernightJS的Typescript,并且在尝试将类导入控制器中时遇到问题,出现错误提示:
错误:找不到模块'@ Models / company'
以某种方式,当我将导入更改为“ ../models/company”时,它可以工作。同样,导入“ @ Models / firebase”也可以。
UserController:
import { Controller, Get } from '@overnightjs/core'
import { Request, Response } from 'express'
import CompanyModel from '@Models/company'
@Controller('api/company')
export class CompanyController {
private readonly company = new CompanyModel()
@Get()
private async get(req: Request, res: Response): Promise<any> {
const companies = await this.company.getAllCompanies()
return res.status(200).json({
data: {
companies
}
})
}
}
CompanyModelClass:
import * as firebaseAdmin from 'firebase-admin'
import { Firestore, QuerySnapshot, QueryDocumentSnapshot } from '@Models/firebase'
class CompanyModel {
private readonly firestore: Firestore
constructor () {
this.firestore = firebaseAdmin.firestore()
}
async getAllCompanies() {
return await new Promise((resolve: any) => {
this.firestore.collection('/companies').onSnapshot((snapshot: QuerySnapshot) => {
const docs = snapshot.docs.map((doc: QueryDocumentSnapshot) => {
return doc.data()
})
resolve(docs)
})
})
}
}
export default CompanyModel
tsconfig:
{
"compilerOptions": {
"target": "es5",
"baseUrl": "./src",
"allowJs": true,
"module": "commonjs",
"moduleResolution": "node",
"paths": {
"@Models/*": ["models/*"]
},
"experimentalDecorators": true,
"resolveJsonModule": true,
"esModuleInterop": true,
"emitDecoratorMetadata": true,
"importHelpers": true,
"lib": [
"es2015"
],
"types": [
"node"
],
"sourceMap": true
},
"include": [
"./src/**/*"
],
"exclude": [
"node_modules"
]
}
package.json:
{
"name": "api-with-overnightjs",
"scripts": {
"dev": "nodemon --watch 'src/**/*.ts' --ignore 'src/**/*.spec.ts' --exec 'ts-node' src/index.ts",
"deploy": "now --target production"
},
"dependencies": {
"@overnightjs/core": "^1.4.7",
"@overnightjs/jwt": "^1.1.2",
"babel-plugin-import": "^1.11.0",
"ejs": "^2.6.1",
"express": "^4.16.4",
"express-jwt": "^5.3.1",
"firebase-admin": "^7.3.0",
"typescript": "^3.4.4"
},
"devDependencies": {
"@types/express": "^4.16.1",
"@types/express-jwt": "^0.0.42",
"@typescript-eslint/eslint-plugin": "^1.7.0",
"@typescript-eslint/parser": "^1.7.0",
"babel-eslint": "^10.0.1",
"eslint": "^5.16.0",
"eslint-config-standard": "^12.0.0",
"eslint-plugin-import": "^2.17.2",
"eslint-plugin-node": "^8.0.1",
"eslint-plugin-promise": "^4.1.1",
"eslint-plugin-security": "^1.4.0",
"eslint-plugin-standard": "^4.0.0",
"module-alias": "^2.2.0",
"nodemon": "^1.18.11",
"ts-node": "^8.1.0",
"tsconfig-paths": "^3.8.0"
}
}
非常感谢!
答案 0 :(得分:0)
将tsconfig-paths添加到运行脚本中后,问题解决了:
"dev": "nodemon --watch 'src/**/*.ts' --ignore 'src/**/*.spec.ts' --exec 'ts-node' -r tsconfig-paths/register src/index.ts"