ts-node throw an error: cannot find module, although it has it in tsconfig

Viewed 1091

I have monorepo with lerna, and I want to run typescript file (in scripts folder, not a package).

the problem is when I run the file (bar.ts)- it access to some files in the project and throw an error:

Error: Cannot find module '@packages/roles'
Require stack:
- C:\..\prj\packages\pck1\src\foo.ts
- C:\..\prj\packages\pck1\src\index.ts
- C:\..\prj\scripts\bar.ts
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:1029:15)
    at Function.Module._load (internal/modules/cjs/loader.js:898:27)
    at Module.require (internal/modules/cjs/loader.js:1089:19)
    at require (internal/modules/cjs/helpers.js:73:18)

I run the command from the root project:

ts-node scripts/bar

I also have tsconfig.json file (in the root project):

{
  "ts-node": {
    "compilerOptions": {
      "target": "es5",
      "module": "commonjs",
      "baseUrl": ".",
      "paths": {
        "@packages/*": ["packages/*/src"]
      }
    }
  },
  "compilerOptions": {
    "noEmit": true,
    "strict": false,
    "jsx": "react",
    "target": "esnext",
    "module": "esnext",
    "moduleResolution": "node",
    "esModuleInterop": true,
    "removeComments": true,
    "rootDir": ".",
    "baseUrl": ".",
    "lib": ["esnext", "dom"],
    "types": ["webpack-env", "node"],
    "paths": {
      "@packages/*": ["packages/*/src"]
    }
  },
  "include": ["packages", "apps", "jest-dom/extend-expect"],
  "exclude": ["node_modules", "dist"]
}

In every packages/*/src I have index.ts that expose everything I need.

Not sure what I missing here. why ts-node can't resolve the file?

0 Answers
Related