最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - VS Code shows module not found even though WebPack build works - Stack Overflow

programmeradmin1浏览0评论

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 plaining about the import.

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 plaining about the import.

Share Improve this question edited Oct 26, 2020 at 21:28 Juan 5,0703 gold badges40 silver badges48 bronze badges asked Oct 25, 2020 at 20:06 NeminusNeminus 1411 silver badge6 bronze badges 2
  • native imports (not using a bundler like Webpack) require a valid relative/absolute path and the file extension. A path alias is a Webpack thing. – Randy Casburn Commented Oct 25, 2020 at 20:22
  • 1 Does this answer your question? Make VS code read webpack.config and recognize path with alias? – Randy Casburn Commented Oct 25, 2020 at 20:24
Add a ment  | 

1 Answer 1

Reset to default 7

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.

发布评论

评论列表(0)

  1. 暂无评论