typescript 无法找到tsconfig路径

ezykj2lf  于 2023-01-21  发布在  TypeScript
关注(0)|答案(3)|浏览(399)

我正在使用一个nodejs应用程序,我想在tsconfig.json中配置我的路径,但我遇到了路径配置问题,我一直收到错误Error: Cannot find module '@environment'
配置

{
    "compilerOptions": {
      "resolveJsonModule" : true,
      "target": "es6",
      "module": "commonjs" ,
      "lib": [
        "dom",
        "es6"
      ],
      "outDir": "build",
      "rootDir": "src",
      "removeComments": true ,
      "strict": true,
      "noImplicitAny": true,
      "baseUrl": "./",
      "paths": {
        "@lime/*": ["src/*"],
        "@environment": ["src/config/environment.ts"],
      },
      "esModuleInterop": true 
    }
  }

项目树:

src - config - environment.ts
    ...
    - index.js
package.json
tsconfig.json
...

environment.ts

import * as dotenv from 'dotenv';

dotenv.config();

interface Environment {
    port: number | string,
    baseUrl: string,
    databaseUri: string,
}

export const environment: Environment = {
    port: process.env.PORT!,
    baseUrl: process.env.BASE_URL!,
    databaseUri: process.env.DATABASE_URI!
}

index.ts中,我导入了环境。ts作为import { environment } from '@environment';,请说明可能出现了什么错误?

wbgh16ku

wbgh16ku1#

如果您尝试直接使用nodets-node执行此操作,则应注意默认情况下tsconfig路径不按节点解析。如果您使用tsc进行构建(而不是使用webpack或类似工具生成bundle),则可以将依赖项tsconfig-paths添加到依赖项中,如下所示:

npm install --save tsconfig-paths

然后使用以下命令执行代码:

node -r tsconfig-paths/register dist/index.js

如果您将TS代码直接用于ts-node,则可以用途:

ts-node -r tsconfig-paths/register src/index.ts

在生产中,建议将源代码与webpack捆绑在一起,并在捆绑时使用tsconfig-paths-webpack-plugin)这样的插件来解析路径。

yruzcnhs

yruzcnhs2#

我可能不确定,我检查了文档。似乎路径应该是目录。请尝试这个组合。

{

      "baseUrl": "./",
      "paths": {
        "@lime/*": ["src/*"],
        "@environment": ["src/config/"],
      },
      "esModuleInterop": true 
    }
  }

并导入如下内容:
import { environment } from '@environment/environment'

q35jwt9p

q35jwt9p3#

tsconfig.json

// prettier-ignore
{
  "extends": "@tsconfig/react-native/tsconfig.json",     /* Recommended React Native TSConfig base */
  "compilerOptions": {
    /* Visit https://aka.ms/tsconfig.json to read more about this file */

    /* Completeness */
    "skipLibCheck": true,                                 /* Skip type checking all .d.ts files. */

    /* Path */
    "baseUrl": "./src",
    "paths": {
      "@/*": ["./*"],
      "@assets/*": ["assets/*"],
      "@components/*": ["components/*"],
      "@hooks/*": ["hooks/*"],
      "@lib/*": ["lib/*"],
      "@ui/*": ["ui/*"],
    }
  },
  "exclude": [
    "node_modules",
    "babel.config.js",
    "metro.config.js",
    "jest.config.js"
  ]
}

babel.config.js

module.exports = {
  presets: ['module:metro-react-native-babel-preset'],
  plugins: [
    [
      'module-resolver',
      {
        root: ['./src'],
        extensions: [
          '.ios.ts',
          '.android.ts',
          '.ts',
          '.ios.tsx',
          '.android.tsx',
          '.tsx',
          '.jsx',
          '.js',
          '.json',
        ],
        alias: {
          '@': './src',
          '@assets': './src/assets',
          '@components': './src/components',
          '@hooks': './src/hooks',
          '@lib': './src/lib',
          '@ui': './src/ui',
        },
      },
    ],
  ],
};

相关问题