简体   繁体   English

VS Code 显示模块未找到,即使 WebPack 构建有效

[英]VS Code shows module not found even though WebPack build works

My VS Code says that it can't find an import even though my WebPack build still works.我的 VS Code 说它找不到导入,即使我的 WebPack 构建仍然有效。

Here is the import...这里是进口...

import * as tf from '@tensorflow/tfjs';

and the message from VS Code:以及来自 VS Code 的消息:

Cannot find module '@tensorflow/tfjs'.找不到模块“@tensorflow/tfjs”。 Did you mean to set the 'moduleResolution' option to 'node', or to add aliases to the 'paths' option?您是要将“moduleResolution”选项设置为“node”,还是要向“paths”选项添加别名?

I have read something about path aliases which can be set up in the tsconfig.json to shorten long paths to modules.我已经阅读了一些关于路径别名的内容,可以在tsconfig.json中设置这些别名以缩短模块的长路径。 But if this is a path alias and I don't have it configured in my tsconfig.json , how does WebPack know where the module is located?但是,如果这是一个路径别名,而我没有在我的tsconfig.json中配置它,那么 WebPack 如何知道模块位于何处?

I also read that the convention for path aliases is to start with an "@" but the folder in the "node_modules" itself is called "@tensorflow", so i don't know if it really is a path alias and if not, maybe WebPack magically knows that it has to search in "node_modules" for this module?我还读到路径别名的约定是以“@”开头,但“node_modules”本身中的文件夹称为“@tensorflow”,所以我不知道它是否真的是路径别名,如果不是,也许 WebPack 神奇地知道它必须在“node_modules”中搜索这个模块?

As you can see i'm really confused about this and i would be greatfull if somebody could clear this up for me and explain what i must do to stop VS Code from complaining about the import.如您所见,我对此真的很困惑,如果有人可以为我解决这个问题并解释我必须做些什么来阻止 VS Code 抱怨导入,我会很高兴。

Found the solution on my own.我自己找到了解决方案。

I only found stuff about defining the aliases in the tsconfig.json expclicitly in the "path" option, but this couldn't be the answer to my problem because in my other Angular projects there is nothing like this defined even though I'm using @Angular imports there a lot without this problem.我只在“路径”选项中明确地在 tsconfig.json 中找到了有关定义别名的内容,但这不能解决我的问题,因为在我的其他 Angular 项目中,即使我正在使用,也没有像这样定义的东西@Angular 在没有这个问题的情况下导入了很多。

But then I found this in my Angular project "moduleResolution": "node" .但后来我在我的 Angular 项目"moduleResolution": "node"中发现了这个。

As stated in othe typescript documentation:如其他 typescript 文档所述:

However, resolution for a non-relative module name is performed differently.但是,非相对模块名称的解析执行方式不同。 Node will look for your modules in special folders named node_modules. Node 将在名为 node_modules 的特殊文件夹中查找您的模块。

And behold, it works.看,它起作用了。 Yes I could have probably tried this earlier, since its written in the message from VS Code from my question, but i though this was something for only node.js specific projects and I didn't read about this anywhere.是的,我可能早点尝试过这个,因为它写在我的问题的 VS Code 的消息中,但我虽然这只适用于 node.js 个特定项目,但我没有在任何地方读到过这个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM