Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs Typescript react-找不到模块'';反应物化'';path/to/module name.js';隐式具有任何类型的_Reactjs_Webpack_Webpack 2 - Fatal编程技术网

Reactjs Typescript react-找不到模块'';反应物化'';path/to/module name.js';隐式具有任何类型的

Reactjs Typescript react-找不到模块'';反应物化'';path/to/module name.js';隐式具有任何类型的,reactjs,webpack,webpack-2,Reactjs,Webpack,Webpack 2,我正在尝试从react materialize作为导入组件- import {Navbar, NavItem} from 'react-materialize'; 但是,当网页包编译my.tsx时,它会抛出一个错误,如下所示- ERROR in ./src/common/navbar.tsx (3,31): error TS7016: Could not find a declaration file for module 'react-materi alize'. 'D:\Private\W

我正在尝试从react materialize作为导入组件-

import {Navbar, NavItem} from 'react-materialize';
但是,当网页包编译my
.tsx
时,它会抛出一个错误,如下所示-

ERROR in ./src/common/navbar.tsx
(3,31): error TS7016: Could not find a declaration file for module 'react-materi
alize'. 'D:\Private\Works\Typescript\QuickReact\node_modules\react-materialize\l
ib\index.js' implicitly has an 'any' type.
有什么解决办法吗?我不确定如何解析此导入语句以使用
ts加载程序
和网页包

react-materialize的
index.js
如下所示。但是如何在我自己的文件中为模块导入解决这个问题


对于那些想知道我是如何克服这一困难的人。我做了一些黑客的事情

在我的项目中,我创建了一个名为
@types
的文件夹,并将其添加到tsconfig.json中,以便从中查找所有必需的类型。看起来有点像这样-

“类型根”:[
“./node_modules/@types”,
“./@类型”
]
在里面我创建了一个名为
alltypes.d.ts
的文件。从中查找未知类型。对我来说,这些都是未知类型,我把它们加在了那里

declare module 'react-materialize';
declare module 'react-router';
declare module 'flux';

因此,现在typescript不再抱怨找不到的类型了。:)现在的双赢局面:)

我有一个类似的错误,但对我来说是
react router
。通过为其安装类型解决了此问题

npm install --save @types/react-router
错误:

(6,30): error TS7016: Could not find a declaration file for module 'react-router'. '\node_modules\react-router\index.js' implicitly has an 'any' type.

如果您想在站点范围内禁用它,您可以改为编辑
tsconfig.json
,并将
noImplicitAny
设置为
false

,此外,如果您尝试使用的包有自己的类型文件,并且它列在package.json
typings
属性中,则此错误会得到修复

像这样:

{
  "name": "some-package",
  "version": "X.Y.Z",
  "description": "Yada yada yada",
  "main": "./index.js",

  "typings": "./index.d.ts",

  "repository": "https://github.com/yadayada.git",
  "author": "John Doe",
  "license": "MIT",
  "private": true
}

一种更常见的方法是在boot.tsx行中添加

import './path/declare_modules.d.ts';

declare_modules.d.ts中


它可以工作,但其他解决方案更好。

您需要做的就是运行下面的脚本。然后,卸下/重新安装要使用的模块

npm install --save @types/react-redux

我所做的是在项目文件夹目录中从nodejs命令提示符运行以下命令:

  • npm init
  • npm安装-g网页包
  • npm安装--保存react-react-dom@types/react@types/react-dom
  • npm安装--保存dev typescript awesome typescript加载程序源映射加载程序
  • npm安装ajv@^6.0.0
  • npm i react html id
  • 通过添加代码导入App.js文件中的包(在节点模块中):
    import UniqueId from'react html id'

  • 我做了上面的工作(虽然我已经安装了npm),它工作了

    在我的例子中,问题是没有将类型添加到devdependences下的package.json文件中,为了解决这个问题,我运行了
    npm安装--save dev@types/react redux
    注意
    --save dev
    我在react redux类型上也遇到了同样的问题。最简单的解决方案 已添加到tsconfig.json:

    "noImplicitAny": false
    
    例子: 很好用

    npm install @types/react-materialize
    

    如果您正在使用的模块没有
    @types/
    ,您可以通过在上面添加
    /@ts ignore
    注释,即

    // @ts-ignore
    import { Navbar, NavItem } from 'react-materialize';
    
    或者,您可以创建以下缺少的
    @types/


    我在使用typescript向新CRA应用程序添加
    react router dom
    时遇到了这个问题。在我添加了
    @types/react router
    之后,问题得到了解决。

    我也遇到了同样的问题,但不一定与typescript有关,所以我在这些不同的选项上有点困难。我正在使用特定模块
    react门户工具提示创建一个非常基本的create react应用程序,
    出现类似错误:

    找不到模块“react portal tooltip”的声明文件/node_modules/react portal tooltip/lib/index.js“隐式”具有“any”类型。 尝试
    npm install@types/react portal tooltip
    (如果存在),或添加包含
    声明模块“react portal tooltip”的新声明(.d.ts)文件ts(7016)

    我首先尝试了很多步骤-添加各种
    .d.ts
    文件,各种npm安装

    但最终对我起作用的是
    touch src/declare_modules.d.ts
    然后在
    src/declare_modules.d.ts
    中:

    声明模块“反应门户工具提示”;
    
    src/App.js
    中:

    从“反应门户工具提示”导入工具提示;
    //导入“./declare_modules.d.ts”
    
    为了使用这个通用的“声明模块”策略,我在不同的位置遇到了一些困难(我是一个初学者),所以我认为这将适用于不同的选项,但我包含了对我有用的路径

    我最初认为,
    import./declare\u modules.d.ts'
    是必要的。虽然现在看起来不是这样!但我要包括这一步骤,以防它对某人有所帮助


    这是我第一次回答stackoverflow问题,因此我对这里分散的过程表示歉意,希望它仍然有用!:)

    就我而言,我的react有问题,所以我开始做:

    npm安装@types/react

    然后去公园

    npm安装@types/react-dom


    这对我很有效

    尝试添加到tsconfig.json文件:
    “noImplicitAny”:false
    为我工作

    找不到模块“react/jsx runtime”的声明文件

    如果有人出现此错误,您会惊讶地发现,当使用
    create-react-app
    创建react-app时,它只在
    package.json
    中显示
    @types/react
    。但是如果您在
    节点模块/@types
    文件夹中进行检查,将找不到
    react
    的任何文件夹


    解决方案是完全清除
    node\u modules
    文件夹,然后重新安装-
    npm install
    ,或者可以单独安装一个模块-
    npm install@types/react

    我费了好大劲才得到它 在react应用程序中 那里
    npm install @types/react-materialize
    
    // @ts-ignore
    import { Navbar, NavItem } from 'react-materialize';
    
    /// <reference types="react-scripts" />
    declare module 'react-color/lib/components/alpha/AlphaPointer'
    
    
    declare module 'module-name';
    
    yarn add @types/react @types/react-dom @types/react-router-dom -D
    
    npm install @types/react @types/react-dom @types/react-router-dom --save-dev