【问题标题】:Error: Cannot find module '~/module' when using custom pathsError: Cannot find module \'~/module\' when using custom paths
【发布时间】:2022-12-27 22:50:12
【问题描述】:

I have this on my tsconfig.json

"baseUrl": "app",
"paths": {
  "~/*": ["*"]
}

This allows me to import any module as following

import { Foo } from '~/auth'

This works, and tsc compiles without any errors.

However, when I try to run it using node, I get the error

Error: Cannot find module '~/auth'

What can be? I think something is missing because I used to do the same on Next project.

Here is my full tsconfig.json

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "skipLibCheck": true,
    "experimentalDecorators": true,
    "strictPropertyInitialization": false,
    "emitDecoratorMetadata": true,
    "outDir": "dist",
    "baseUrl": "app",
    "paths": {
      "~/*": ["*"]
    }
  },
  "include": ["app/**/*.ts"],
  "exclude": ["node_modules"]
}

【问题讨论】:

    标签: typescript tsc


    【解决方案1】:

    This error occurs because custom Typescript import paths are not replaced at build time by Typescript and cannot be natively resolved by Node.js.

    You must have some kind of transformation that happen at build time, whether using a build tool like Webpack, or some library like tsc-alias (I strongly recommend this option).

    【讨论】:

      猜你喜欢
      • 2022-12-26
      • 2023-01-12
      • 2022-12-26
      • 2022-12-02
      • 2023-01-05
      • 2022-12-27
      • 2018-11-28
      • 2022-12-27
      • 2023-02-04
      相关资源
      最近更新 更多