Javascript Typescript编译器在tsconfig.json上包含文件时未找到绝对路径

Javascript Typescript编译器在tsconfig.json上包含文件时未找到绝对路径,javascript,typescript,visual-studio-code,tsconfig,Javascript,Typescript,Visual Studio Code,Tsconfig,在JS中导入模块时,我尝试使用绝对路径,不幸的是tsc无法正确解析路径,返回错误: 未解析模块名“test2” 我有以下文件结构 project │ └───tsconfig.json └───js │ └───another │ │ └───child │ │ │ test1.js │ │ │ └───some │ │ test2.js 这是test1.jscode: import { sayHi } from "test2"; sayHi

在JS中导入模块时,我尝试使用绝对路径,不幸的是
tsc
无法正确解析路径,返回错误:

未解析模块名“test2”

我有以下文件结构

project
│
└───tsconfig.json
└───js
│   └───another
│   │   └───child
│   │       │  test1.js
│   │
│   └───some
│       │  test2.js
这是
test1.js
code:

import { sayHi } from "test2";
sayHi("John");
function sayHi(user) {
  alert(`Hello, ${user}!`);
}

export { sayHi };
这是
test2.js
code:

import { sayHi } from "test2";
sayHi("John");
function sayHi(user) {
  alert(`Hello, ${user}!`);
}

export { sayHi };
这是我的
tsconfig.json

{
  "compilerOptions": {
    "noEmit": true,
    "allowJs": true
  },
  "include": [
    "js/**/*"
  ],
  "exclude": []
}
当我运行
tsc--traceResolution
时,通过CLI命令可以获得以下信息:

╰─ tsc --traceResolution
======== Resolving module 'test2' from '/Users/Sites/LocalTests/js/another/child/test1.js'. ========
Module resolution kind is not specified, using 'NodeJs'.
Loading module 'test2' from 'node_modules' folder, target file type 'TypeScript'.
Directory '/Users/Sites/LocalTests/js/another/child/node_modules' does not exist, skipping all lookups in it.
Directory '/Users/Sites/LocalTests/js/another/node_modules' does not exist, skipping all lookups in it.
Directory '/Users/Sites/LocalTests/js/node_modules' does not exist, skipping all lookups in it.
Directory '/Users/Sites/LocalTests/node_modules' does not exist, skipping all lookups in it.
Directory '/Users/Sites/node_modules' does not exist, skipping all lookups in it.
Directory '/Users/node_modules' does not exist, skipping all lookups in it.
Directory '/node_modules' does not exist, skipping all lookups in it.
Loading module 'test2' from 'node_modules' folder, target file type 'JavaScript'.
Directory '/Users/Sites/LocalTests/js/another/child/node_modules' does not exist, skipping all lookups in it.
Directory '/Users/Sites/LocalTests/js/another/node_modules' does not exist, skipping all lookups in it.
Directory '/Users/Sites/LocalTests/js/node_modules' does not exist, skipping all lookups in it.
Directory '/Users/Sites/LocalTests/node_modules' does not exist, skipping all lookups in it.
Directory '/Users/Sites/node_modules' does not exist, skipping all lookups in it.
Directory '/Users/node_modules' does not exist, skipping all lookups in it.
Directory '/node_modules' does not exist, skipping all lookups in it.
======== Module name 'test2' was not resolved. ========
如何设置它,使其不仅在目录中搜索,而且在兄弟目录中搜索。查看返回的跟踪,它似乎从未查看过
/some
目录,因此从未找到
test2.js

查看其他帖子,我似乎可以在
tsconfig.json
路径:
部分中添加
/some
的路径,但这意味着我必须手动添加每个路径,这没有意义,因为我计划在这个项目扩展时使用多个目录


另外,如果您想知道,我计划在VScode上使用这个
tsconfig.json
文件来查找绝对路径,并允许其“转到定义”功能正常工作。

这不应该起作用。导入
“test2”
时,TypeScript编译器无法知道在何处查找文件

另外,
“test2”
是一个文件名,而不是绝对路径。要使它成为一个绝对路径,它需要从硬盘的根目录一直到根目录的路径,这显然是一种不好的做法,因为它会使代码具体取决于文件在计算机上的存储位置

TypeScript编译器查找导入的方式是:

  • 如果它是一条相对路径,请遵循它
  • 如果没有,请在
    node\u模块中搜索它
  • 如果没有,请在父文件夹的
    节点\u模块中搜索
  • 继续这样做直到找到为止
所以你需要你的文件有一个相对路径,或者你需要它是一个
npm
模块

一个可能对您有所帮助的功能是,它允许您为TypeScript定义要查找的自定义路径,您可以在这种情况下使用它

例如:

{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "test2": ["some/test2.js"], // This mapping is relative to "baseUrl"
      "some/*": ["some/*"] // You could use this to then import "some/test2"
    }
  }
}

您必须将其添加到tsconfig.json中

{
  "compilerOptions": {
    "baseUrl": "./",
    "paths": {
      "src/*": [
        "./src/*"
      ],
    }
  }
}

从“./../some/test2”
导入{sayHi}有什么不对?VSCode可以在重构时为您管理相对路径。此外,如果有多个名为
test2
的文件,那么预期的结果是什么?没有什么,我最好使用相对路径,但是这个项目的限制(公司规则)只允许绝对路径。这与他们在CI过程中如何设置webpack有关。简而言之,我被迫只使用绝对路径。
test2
不是绝对路径。如果您使用的是绝对路径,那么它可能类似于
/some/test2
。不幸的是,存在这种限制,我不确定您将如何通知TS这种行为(如果“绝对”到相对路径的重新映射是由webpack完成的,则可能不可能)。祝你好运!