VS Code shows module not found even though WebPack build works

懵懂的女人 提交于 2021-01-29 08:18:01

问题


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

Here is the import...

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

and the message from VS Code:

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

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?

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?

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.


回答1:


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.

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

As stated in othe typescript documentation:

However, resolution for a non-relative module name is performed differently. Node will look for your modules in special folders named 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.



来源:https://stackoverflow.com/questions/64528237/vs-code-shows-module-not-found-even-though-webpack-build-works

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!