在TypeScript React中导入图像 - "无法找到模块"

Joh*_*ohn 18 bundler typescript reactjs visual-studio-code parceljs

我正在尝试导入图像以在使用TypeScript的React组件中使用.我正在使用的捆绑包是Parcel(不是Webpack).

.d.ts在项目中创建了一个带有图像文件扩展名的文件,并将其包含在里面tsconfig.json.但是,当我尝试导入图像时,T​​S对我大吼大叫Cannot find module.

我的项目结构:

+ src
  + assets
    - image.jpg
  + components
    - Box.tsx
    - App.tsx
  - index.d.ts
  - index.html
  - index.tsx
- tsconfig.json
- tslint.json
Run Code Online (Sandbox Code Playgroud)

我试着App.tsx像这样导入图像.VS Code强调 '../assets/image.jpg'并说Cannot find module '../assets/image.jpg'.

import * as React from 'react';
import * as img from '../assets/image.jpg';

const Box = props => {
  // do things...
}

export default Box;
Run Code Online (Sandbox Code Playgroud)

我在网上发现的讨论指出需要.d.ts自己定义一个文件,所以我index.d.ts用这一行创建了这个文件.

declare module '*.jpg';
Run Code Online (Sandbox Code Playgroud)

然后加入"include": ["./src/index.d.ts"]里面tsconfig.json,后"compilerOptions" : {...}.

我错过了什么?如何修复TS抛出的错误?

Moh*_*han 46

这个问题可以有很多解决方案

解决方案#1:
使用require包含图像

<img src={require('../../assets/logo.png')} alt="" />
Run Code Online (Sandbox Code Playgroud)

解决方案#2:

而不是将图像导入为 import logo from "../../assets/logo.png"

导入为 const logo = require("../../assets/logo.png");

然后像这样使用它

<img src={logo} alt="" />
Run Code Online (Sandbox Code Playgroud)

解决方案#3: 您的文件夹中
有一个文件。如果您没有看到它,请在名为 的文件夹中创建一个新文件。react-app-env.d.tssrcsrcreact-app-env.d.ts

现在在您的 中react-app-env.d.ts添加此代码。

declare module "*.png";
declare module "*.svg";
declare module "*.jpeg";
declare module "*.jpg";
Run Code Online (Sandbox Code Playgroud)

解决方案#4: 在您的文件夹中
创建。 然后在其中添加以下文本。declaration.d.tssrc

declare module "*.png";
declare module "*.svg";
declare module "*.jpeg";
declare module "*.jpg";
Run Code Online (Sandbox Code Playgroud)

现在你必须让你的tsconfig.json样子像这样:

{
  "compilerOptions": {},
  "include": [
    "src",
    "src/declaration.d.ts",
  ]
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


解决方案#5:

types在您的文件夹中创建一个文件夹src并添加images.d.ts到该文件夹​​中。在其中添加以下文本。

declare module "*.png";
declare module "*.svg";
declare module "*.jpeg";
declare module "*.jpg";
Run Code Online (Sandbox Code Playgroud)

tsconfig.json现在像这样配置:

{
  "compilerOptions": {
    "typeRoots": [
      "node_modules/@types",
      "src/types"
    ],
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述



小智 33

创建模块/类型

# src/types/images.d.ts
declare module '*.jpg';
declare module '*.jpeg';
Run Code Online (Sandbox Code Playgroud)

更改 tsconfig.json

{
    "compilerOptions": {
        "typeRoots" : ["node_modules/@types", "src/types"]
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 我不需要修改 typeRoots,只需将 d.ts 文件转储到项目中即可。 (6认同)

Obi*_*nts 28

对于"react": "^18.2.0"Typescript,请执行以下操作:

  1. declaration.d.ts在您的目录中创建一个名为:的文件src/
  2. 添加这行代码:

declare module "*.jpg"
declare module "*.png"

Run Code Online (Sandbox Code Playgroud)

就这样,错误应该消失了。

祝你好运!


cr4*_*r4z 24

这花了我大约一个小时,所以我将留下对我有用的解决方案。

Matt McCutchen 的解决方案对我来说只起到了一半作用,但我仍然面临 VSCode 错误。为了解决这个问题,我将 tsconfig.json 和 index.d.ts 移动到根目录,而不是 src。现在您希望 tsconfig.json 看起来像这样:

"include": ["src", "index.d.ts"]
Run Code Online (Sandbox Code Playgroud)

对于你的index.d.ts,这个:

declare module '*.png';
declare module '*.jpg';
Run Code Online (Sandbox Code Playgroud)

(^根据您的打字需求进行调整)

  • “为了解决这个问题,我将 index.d.ts 移动到根目录,而不是 src”。这也是我的问题。谢谢! (3认同)

Mat*_*hen 19

如果你从字面上写"include": ["./src/index.d.ts"]tsconfig.json,你没有"files"设定,这意味着通过定义的项目tsconfig.json仅包括单个文件./src/index.d.ts.当您在VS代码中打开任何其他文件时,VS代码使用不使用您的单独的语言服务实例tsconfig.json.调整"include"设置以匹配所有.ts,并.tsx在你的项目文件,或只是将其删除,并依靠包括含有该目录下的所有文件的默认行为tsconfig.json.

第2轮

TypeScript是忽略的,index.d.ts因为它假定它index.d.ts是从最新生成的,index.tsx并且index.tsx更可能是最新的.将您的index.d.ts文件命名为其他内容,例如declaration.d.ts.

  • 您的第二轮是不断赠送的礼物。您刚刚解决了我的 index.d.ts 和 SVG 问题,所以谢谢! (4认同)
  • 哇,这太神奇了,非常感谢!!我几乎花了整个早上试图弄清楚为什么它不起作用:))) (3认同)
  • 我确实从字面上写了 `"include": ["./src/index.d.ts"]` 。现在我已经删除了该行并重新启动了 VS Code,但问题仍然存在(`找不到模块'../assets/image.jpg'`)。问题可能是由捆绑程序引起的吗?但是如果 VS Code 使用单独的语言服务,我认为使用哪个 bundler 并不重要。 (2认同)
  • 没关系,我想我发现了问题。请参阅更新的答案。 (2认同)

小智 10

在文件夹中创建index.d.ts文件src,并添加此行

declare module '*.jpg';
Run Code Online (Sandbox Code Playgroud)

  • 有效,但您能告诉我**为什么以及如何**这有效吗?这是否必须与 typescript 相关,还是与 vscode 相关?这就像忽略所有类似 linting 问题中的错误吗?请澄清。谢谢。 (8认同)
  • 奇怪的是,如果我关闭 vs code 中的index.d.ts 文件。然后错误返回。只有当文件打开时它才起作用。有谁知道为什么? (4认同)

小智 8

使用 create-react-app 时应该创建一个名为“react-app-env.d.ts”的文件。该文件将所有类型的图像文件和更多类型的图像文件分类为可以导入的模块。文件内应有以下内容:

/// <reference types="react-scripts" />
Run Code Online (Sandbox Code Playgroud)


小智 6

例如,有一个解决方法,

import logo from "../../assets/logo.png"

Run Code Online (Sandbox Code Playgroud)

将此更改为

const logo =  require("../../assets/logo.png")
Run Code Online (Sandbox Code Playgroud)

  • 会给你一个错误,比如“require语句不是导入语句的一部分” (8认同)
  • 如果您使用的是 React &lt; 17.0.1,则 require 可以工作,17.0.1 及更高版本使用 import (2认同)

Mat*_*abe 5

我声明了多种图像类型,并且必须将它们放入单独的 .d.ts 文件中以使 VS Code 满意:

src/types/image.jpg.d.ts:

declare module '*.jpg' {
  import { ImageSourcePropType } from 'react-native'

  const content: ImageSourcePropType

  export default content
}
Run Code Online (Sandbox Code Playgroud)

src/types/image.png.d.ts:

declare module '*.png' {
  import { ImageSourcePropType } from 'react-native'

  const content: ImageSourcePropType

  export default content
}
Run Code Online (Sandbox Code Playgroud)

src/types/image.svg.d.ts:

declare module '*.svg' {
  import React from 'react'
  import { SvgProps } from 'react-native-svg'

  const content: React.FC<SvgProps>

  export default content
}
Run Code Online (Sandbox Code Playgroud)

无需对 tsconfig 等进行调整。