admin管理员组文章数量:1221309
I am writing vanilla Javascript but using the Typescript compiler's checkJs
option to do type checking in VSCode. I have Webpack set up to load various asset types (CSS, images, etc), which works fine for builds, but Code is treating these statements as an error. For example, in this code
require("bootstrap");
require("bootstrap/dist/css/bootstrap.css");
var img = require("../img/image.png");
the first line is fine but the next two both show an error under the (string) argument to require()
, with the tooltip "Cannot find module (name)".
I have installed @types/webpack
and @types/webpack-env
, which fixed resolve()
and resolve.context()
. Am I missing another typings package or is this an issue I need to take up on the DT issue tracker?
I am writing vanilla Javascript but using the Typescript compiler's checkJs
option to do type checking in VSCode. I have Webpack set up to load various asset types (CSS, images, etc), which works fine for builds, but Code is treating these statements as an error. For example, in this code
require("bootstrap");
require("bootstrap/dist/css/bootstrap.css");
var img = require("../img/image.png");
the first line is fine but the next two both show an error under the (string) argument to require()
, with the tooltip "Cannot find module (name)".
I have installed @types/webpack
and @types/webpack-env
, which fixed resolve()
and resolve.context()
. Am I missing another typings package or is this an issue I need to take up on the DT issue tracker?
1 Answer
Reset to default 18Requiring non JS or TS resources is currently not supported by the TypeScript server which powers VS Code's JavaScript and TypeScript intellisense. Here's the issue tracking this: https://github.com/Microsoft/TypeScript/issues/15146
As a workaround, try creating a d.ts
file in your project with the content:
declare module '*.css' { export default '' as string; }
declare module '*.png' { export default '' as string; }
You can also suppress individual errors by adding // @ts-ignore
before the require:
// @ts-ignore
var img = require("../img/image.png");
本文标签:
版权声明:本文标题:javascript - Typescript compiler "cannot find module" when using Webpack require for CSSimage assets - Stack O 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1739317666a2157880.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
//@ts-ignore
so I'm trying to fix it "correctly" – Coderer Commented Aug 1, 2017 at 10:02