typescript - VSCode 导入具有相对路径的节点模块

标签 typescript visual-studio-code

我以某种方式让我的工作区进入了 VSCode 提供使用模块 @types 的相对路径为节点模块添加导入的状态:

VSCode offering to import 'React' from '../../../../node_modules/@types/react' rather than 'react'

当我们添加 paths(如此处所述)时,它开始了,但恢复这些更改似乎没有帮助(即使在“重新加载 TS 项目”或什至重新加载 VS 代码之后)。

对可能导致此问题的原因有任何想法吗?

VSCode 的“导入模块说明符”设置为auto

最佳答案

如果您的工作区使用的是 Typescript 2.9.2,那么可能是这个问题:

https://github.com/Microsoft/vscode/issues/53597

您可以在右下方的底部栏中看到 typescript 的版本。如果单击它,您将看到您已经可以使用的 TS 版本。

see the typescript version

建议的解决方法是将工作区使用的 TypeScript 版本降级到 2.7.2。或者升级到 typescript@next

关闭此错误的 Microsoft 工程师表示:

This has been fixed in VS Code insiders. On the current version of VS Code, you can also upgrade your workspace to use Typescript 3.0. We did not backport the fix due to lack of testing, engineering cost, and concerns about causing other regressions.

此外,我已经测试过将 2.7.2 和 typescript@next 安装到我的本地 node_modules 中。两者都解决了问题。

希望对您有所帮助!

关于typescript - VSCode 导入具有相对路径的节点模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51433140/

相关文章:

javascript - 如何在我的 angular4 应用程序中使用输入类型日期?当前浏览器是 mozilla firefox

angular - Typescript 将 null 分配给类型不会触发错误

debugging - 如何在 Visual Studio Code 中的 launch.json 中扩展 $PATH?

visual-studio-code - Visual Studio Code 自定义缩进样式

Angular:将数据从组件发送到另一个组件

javascript - typescript 使用 map 进行迭代并使用索引获取键,值和删除项目

visual-studio-code - 测试 VSCode 扩展 - 如何验证是否设置了装饰

c# - 使用 VSCode 在调试 session 中间移动断点

node.js - 如何使用 nestjs 和 socket.io 创建房间

javascript - NodeJS 未处理的 promise 拒绝问题