typescript 导入别名无法解析导入

问题描述 投票:0回答:1

我正在尝试创建使用绝对路径的别名。但我不知道如何正确运行它。我已经尝试过 module-alias 包,但我认为它不适用于 es 模块。我总是收到此错误:

从 /Users/alikocak/typetest/dist/main.js 导入的包 /Users/alikocak/typetest/package.json 中未定义包导入说明符“#utils/math”

tsconfig.json

{
    "compilerOptions": {
      "module": "ESNext",
      "target": "ESNext",
      "moduleResolution": "node",
      "outDir": "./dist",
      "rootDir": "./src",
      "baseUrl": ".",
      "paths": {
        "#utils/*": ["src/utils/*"]
      }
    }
  }

package.json

{
  "name": "typetest",
  "version": "1.0.0",
  "export": "dist/main.js",
  "type": "module",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node dist/main.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "description": "",
  "imports": {
    "#utils/*": "./dist/utils/*"
  }


}
typescript import path package module-alias
1个回答
0
投票

我将 package.json 导入更改为:

"imports": {
    "#utils/*": "./dist/utils/*.js"
 }

实际上我一开始就尝试过这个,但不知何故它不起作用,但现在它在该配置下工作得很好。

© www.soinside.com 2019 - 2024. All rights reserved.