即使 WebPack 构建有效,VS Code 仍显示未找到模块 [英] VS Code shows module not found even though WebPack build works

查看:30
本文介绍了即使 WebPack 构建有效,VS Code 仍显示未找到模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 VS Code 说即使我的 WebPack 构建仍然有效,它也找不到导入.

My VS Code says that it can't find an import even though my WebPack build still works.

这里是导入...

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

以及来自 VS Code 的消息:

and the message from VS Code:

找不到模块@tensorflow/tfjs".您是想将 'moduleResolution' 选项设置为 'node',还是要向 'paths' 选项添加别名?

Cannot find module '@tensorflow/tfjs'. Did you mean to set the 'moduleResolution' option to 'node', or to add aliases to the 'paths' option?

我已经阅读了一些关于路径别名的内容,这些别名可以在 tsconfig.json 中设置以缩短模块的长路径.但是如果这是一个路径别名而我没有在我的 tsconfig.json 中配置它,那么 WebPack 如何知道模块所在的位置?

I have read something about path aliases which can be set up in the tsconfig.json to shorten long paths to modules. 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?

我还读到路径别名的约定是以@"开头的.但是node_modules"中的文件夹它本身被称为@tensorflow",所以我不知道它是否真的是路径别名,如果不是,也许 WebPack 神奇地知道它必须在node_modules"中搜索.对于这个模块?

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?

如您所见,我对此感到非常困惑,如果有人能为我解决这个问题并解释我必须做些什么来阻止 VS Code 抱怨导入,我会非常高兴.

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.

推荐答案

我自己找到了解决方案.

Found the solution on my own.

我只在路径"中找到了关于在 tsconfig.json 中明确定义别名的内容.选项,但这不能解决我的问题,因为在我的其他 Angular 项目中,即使我在那里使用 @Angular 导入很多没有这个问题的定义,也没有类似的定义.

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.

但后来我在我的 Angular 项目"moduleResolution":"node"中发现了这个.

But then I found this in my Angular project "moduleResolution": "node".

如其他打字稿文档所述:

As stated in othe typescript documentation:

但是,对非相关模块名称的解析是不同的.Node 将在名为 node_modules 的特殊文件夹中查找您的模块.

However, resolution for a non-relative module name is performed differently. Node will look for your modules in special folders named node_modules.

看哪,它有效.是的,我可能早点尝试过这个,因为它写在我的问题中来自 VS Code 的消息中,但我虽然这只是针对 node.js 特定项目的东西,我没有在任何地方读到这个.

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.

这篇关于即使 WebPack 构建有效,VS Code 仍显示未找到模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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