简体   繁体   English

Typescript - 具有路径别名的导入类失败

[英]Typescript - Import class with path alias failed

I'm in the middle of trying Typescript with OvernightJS and having problem trying to import a class into my controller, got an error saying : 我正在尝试使用OvernightJS编写Typescript并且在尝试将类导入我的控制器时遇到问题,得到一个错误说:

Error: Cannot find module '@Models/company' 错误:找不到模块'@ Models / company'

Somehow when I changed the import to '../models/company' and it works. 不知怎的,当我将导入更改为'../models/company'并且它有效。 Also importing '@Models/firebase' works fine too. 同时导入'@ Models / firebase'也可以正常工作。

UserController : 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 : 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 : 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 : 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"
  }
}

Thanks and appreciate a lot! 非常感谢和欣赏!

tsconfig-paths添加到运行脚本时,问题得以解决:

"dev": "nodemon --watch 'src/**/*.ts' --ignore 'src/**/*.spec.ts' --exec 'ts-node' -r tsconfig-paths/register src/index.ts"

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM