0

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 :

Error: Cannot find module '@Models/company'

Somehow when I changed the import to '../models/company' and it works. Also importing '@Models/firebase' works fine too.

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

Thanks and appreciate a lot!

4
  • and what is your question? Commented Apr 22, 2019 at 13:46
  • how do i import class in typescript? did i missed out any typescript related config? Commented Apr 22, 2019 at 13:55
  • Just to confirm - file name of CompanyModel is company.ts? Commented Apr 22, 2019 at 14:25
  • 1
    yes it is. i've just found the problem, need to add tsconfig-paths into run script so that custom paths get registered. appreciate the feedback still. :) Commented Apr 22, 2019 at 14:36

1 Answer 1

2

The issue solved when added tsconfig-paths into run scripts :

"dev": "nodemon --watch 'src/**/*.ts' --ignore 'src/**/*.spec.ts' --exec 'ts-node' -r tsconfig-paths/register src/index.ts"
Sign up to request clarification or add additional context in comments.

1 Comment

you could try npmjs.com/package/tsmon which is very fast on reloading tsmon src/index.ts.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.