Node Error: Cannot find module - typescript api for custom module where I export mongoose schemas and interfaces

Viewed 123

I have a typescript module @tlabs/models where I'm simply exporting in index.ts:

export * from '../models......'

where in each file I have something like:

export const Project = typedModel('projects', ProjectSchema);

and my only dependency is ts-mongoose imported in each file simply as:

import { createSchema, Type, typedModel, ExtractProps } from 'ts-mongoose';

ts-mongoose being a dependency that itself required mongoose + mongoose types.

In my typescript node project I have ts-mongoose, mongoose and @tlabs/models as dependencies and @types/mongoose as dev dependency.

Running tsc is fine, the files get compiled and there's no error being thrown out but then trying to run the actual files throws out:

Error: Cannot find module '@tlabs/models'

I have reinstalled all modules several times and checked package.json as well as the actual files on the disk + through vscode and they're right there.

What am I missing out?

My tsconfig is:

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

1 Answers

My final TS config for my exported models:

{
    "compilerOptions": {
        "target": "es5",
        "module": "commonjs",
        "declaration": true,
        "declarationMap": true,
        "sourceMap": true,
        "outDir": "lib",
        "strict": true,
        "esModuleInterop": true
    },
    "include": ["src"]
}

and relevant package.json configuration:

{
    "main": "lib/index.js",
    "types": "lib",
    "scripts": {
        "tsc": "tsc",
        "build": "tsc -p ."
    },
    "files": [
        "lib"
    ],
}
Related