TypeScript路径映射“无法找到映射的模块a/a". [英] TypeScript path mapping "Cannot find module a-mapped/a"

查看:402
本文介绍了TypeScript路径映射“无法找到映射的模块a/a".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个结构如下的项目:

I have a project with the following structure:

┌ tsconfig.json
│ {
│   "compilerOptions": {
│     "baseUrl": ".",
│     "paths": { "a-mapped/*": ["a/*"] }
│   }
│ }
│
├ a
│ └─ a.ts
│    export const a = 1;
└ b
  └─ b.ts
     import { a } from "a-mapped/a";
     export const b = a + 1;

当我运行tsc时,生成的b.js包含:

When I run tsc, the resulting b.js contains:

var a_1 = require("a-mapped/a");
exports.b = a_1.a + 1;

如果尝试使用node运行它,则会收到错误找不到模块a-mapped/a".

If I try to run it with node, I get the error "Cannot find module a-mapped/a".

我希望tsc../a/a而不是a-mapped/a生成导入.我想念什么或做错什么了?

I expected tsc to generate an import from ../a/a, not a-mapped/a. What am I missing or doing wrong?

推荐答案

我不久前遇到了这个问题.有趣的是,打字稿将理解路径映射,但将它们按原样保留在已编译的javascript

I had this problem myself not too long ago. Interestingly enough, typescript will understand path mappings, but keep them as-is in the compiled javascript by design.

编译器不会重写模块名称.模块名称被视为资源标识符,并按照它们在源代码中出现的方式映射到输出中

The compiler does not rewrite module names. module names are considered resource identifiers, and are mapped to the output as they appear in the source

您编写的模块名称不会在输出中更改. 路径"和"baseURL"在那里告诉编译器它们在运行时的位置. https://github.com/Microsoft/TypeScript/issues/9910#issuecomment- 234729007

The module names you write are not going to change in the output. the "paths" and "baseURL" are there to tell the compiler where they are going to be at runtime. https://github.com/Microsoft/TypeScript/issues/9910#issuecomment-234729007

有几种解决方法.

您可以使用ts-node而不是node来运行您的项目.

You can use ts-node instead of node to run your project.

专家:

  • 易于使用,没有麻烦.

缺点:

  • 例如,如果您正在编写浏览器代码,库或无法控制运行时环境,则可能无法执行此操作.
  • 性能可能是个问题,尤其是启动时间.
  • you might not be able to do this, for example if you're writing browser code, a library, or don't control the runtime environment.
  • Performance might be an issue, especially startup times.

或者,您可以使用打字稿转换器使编译器输出正确的javascript文件(例如 @ zerollup/ts-transform-paths ttypescript ).

Alternatively you can use typescript transformers to make the compiler output correct javascript files (for example @zerollup/ts-transform-paths and ttypescript).

专家:

  • 编译速度更快,可以可靠地使用ttsc --watch.

注意:与--watch相比,我已经测试了该设置及其比使用任何工具都要快的速度.

Note: I have tested this setup and its much faster than using any tools when using --watch.

缺点:

  • 至少到目前为止,您需要使用像ttypescript这样的打字稿包装器.
  • 设置起来有些困难.

最后,您可以使用一个工具来修复生成的javascript中的路径( module-alias ef-tspm ).

Finally, you can use a tool that would fix the paths for you in your generated javascript (ts-module-alias, module-alias, ef-tspm).

专家:

  • 使用纯净的打字稿.
  • 易于设置(编译后只需运行该工具即可!)

缺点:

  • 可能会导致编译时间变慢.
  • 观看操作更难设置.

我最终使用ef-tspm来修复文件,尽管我对构建时间不完全满意,但它通常可以正常工作,并且可能值得探讨一下转换器和ttypescript.如果有帮助,我创建了一个打字稿/节点项目,并设置了路径别名.

I ended up using ef-tspm to fix the files, and it generally works, although I'm not fully satisfied with the build times, and it's probably worth exploring transformers and ttypescript. In case it helps, I have created a typescript / node project with path aliases set-up.

这篇关于TypeScript路径映射“无法找到映射的模块a/a".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆