Typescript react - Could not find a declaration file for module ''react-materialize'. 'path/to/module-name.js' implicitly has an any type
Asked Answered
N

27

260

I am trying to import components from react-materialize as -

import {Navbar, NavItem} from 'react-materialize';

But when the webpack is compiling my .tsx it throws an error for the above as -

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.

Is there any resolution for this? I'm unsure how to resolve this import statement to work with ts-loader and webpack.

The index.js of react-materialize looks likes this. But how do I resolve this for the module import in my own files?

https://github.com/react-materialize/react-materialize/blob/master/src/index.js

Nl answered 4/1, 2017 at 11:29 Comment(5)
You need to install the typings/types for the package (npm install @types/react-materialize). However, it looks like this doesn't exist yet, so you may need to create it yourself. I found this article helpful - templecoding.com/blog/2016/03/31/…Joselyn
@T Mitchell so does that mean any kind of types which we are missing we never would be able to compile webpack without this creating these types ? There should be some way ?Nl
There is a way - #38224732Joselyn
this seems like a bit of a workaround to me.Coffer
Possible duplicate of Could not find a declaration file for module 'module-name'. '/path/to/module-name.js' implicitly has an 'any' typeMossy
N
151

For those who wanted to know that how did I overcome this . I did a hack kind of stuff .

Inside my project I created a folder called @types and added it to tsconfig.json for find all required types from it . So it looks somewhat like this -

"typeRoots": [
  "../node_modules/@types",
  "../@types"
]

And inside that I created a file called alltypes.d.ts . To find the unknown types from it . so for me these were the unknown types and I added it over there.

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

So now the typescript didn't complain about the types not found anymore . :) win win situation now :)

Nl answered 13/1, 2017 at 9:48 Comment(9)
I missed the part about it being in a separate file. Seems like putting those declare statements in a file named index.d.ts fixed it for me. I didn't have to touch typeRoots.Configuration
That works just fine but it causes issues when using require for other stuff like images. Basically, it just doesn't allow you to use require for anything.Shiva
this solution no longer works with latest TS. nvalid module name in augmentation. Module 'some-dumb-lib-with-no-types' resolves to an untyped module at '/path/to/module', which cannot be augmented.Rusticus
Something also worth mentioning: depending on the location of your source, you may also need to add "../types" to your "includes" arrayGausman
but when I run the app it is complaining with the same error message, how to resolve it?Recitative
If you would like to disable it site wide you can instead edit tsconfig.json and set noImplicitAny to false.Klee
I have issues with react-countdown-clock, created @types folder and did all the above steps. still getting the same error.Cacophonous
This all worked for me except for the additional config added to the tsconfig.json. Rather than using the example in the answer, I had to set the @types location in the "paths" value rather than the "typeRoots" value, i.e. { "compilerOptions": { "target": "es5", "baseUrl": "./", "paths": { "" : ["src/@t`ypes/"] } } }Twobit
anyone has luck with the latest version of ts?Florafloral
A
205

I had a similar error but for me it was react-router. Solved it by installing types for it.

npm install --save @types/react-router

Error:

(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.

If you would like to disable it site wide you can instead edit tsconfig.json and set noImplicitAny to false.

Armour answered 20/4, 2017 at 14:40 Comment(5)
Although there is currently no such @types/react-navigation-tabs package yet, still the mentioned "noImplicitAny": false, did disable it (I got this issue because of using react-native init MyProject --template typescript, I think so at least) 👍Umbrage
or yarn add @types/react-router --devJingoism
yarn add @types/react-router-dom Fixed it for me when using create-react-appFrisch
Best answer. Had the same issue with react-slick as my project is a TypeScript project. Fix npm i --save-dev @types/react-slickKaveri
I had a similar problem with jest-axe. Solved by installing @types/jest-axe.Disintegrate
N
151

For those who wanted to know that how did I overcome this . I did a hack kind of stuff .

Inside my project I created a folder called @types and added it to tsconfig.json for find all required types from it . So it looks somewhat like this -

"typeRoots": [
  "../node_modules/@types",
  "../@types"
]

And inside that I created a file called alltypes.d.ts . To find the unknown types from it . so for me these were the unknown types and I added it over there.

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

So now the typescript didn't complain about the types not found anymore . :) win win situation now :)

Nl answered 13/1, 2017 at 9:48 Comment(9)
I missed the part about it being in a separate file. Seems like putting those declare statements in a file named index.d.ts fixed it for me. I didn't have to touch typeRoots.Configuration
That works just fine but it causes issues when using require for other stuff like images. Basically, it just doesn't allow you to use require for anything.Shiva
this solution no longer works with latest TS. nvalid module name in augmentation. Module 'some-dumb-lib-with-no-types' resolves to an untyped module at '/path/to/module', which cannot be augmented.Rusticus
Something also worth mentioning: depending on the location of your source, you may also need to add "../types" to your "includes" arrayGausman
but when I run the app it is complaining with the same error message, how to resolve it?Recitative
If you would like to disable it site wide you can instead edit tsconfig.json and set noImplicitAny to false.Klee
I have issues with react-countdown-clock, created @types folder and did all the above steps. still getting the same error.Cacophonous
This all worked for me except for the additional config added to the tsconfig.json. Rather than using the example in the answer, I had to set the @types location in the "paths" value rather than the "typeRoots" value, i.e. { "compilerOptions": { "target": "es5", "baseUrl": "./", "paths": { "" : ["src/@t`ypes/"] } } }Twobit
anyone has luck with the latest version of ts?Florafloral
H
96

I've had a same problem with react-redux types. The simplest solution was add to tsconfig.json:

"noImplicitAny": false

Example:

{
  "compilerOptions": {
    "allowJs": true,
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true,
    "isolatedModules": true,
    "jsx": "react",
    "lib": ["es6"],
    "moduleResolution": "node",
    "noEmit": true,
    "strict": true,
    "target": "esnext",
    "noImplicitAny": false,
  },
  "exclude": ["node_modules", "babel.config.js", "metro.config.js", "jest.config.js"]
}
Harl answered 2/4, 2019 at 15:26 Comment(6)
this worked great in IntelliJ IDE, but the custom typeRoots method (which is the accepted answer) did even work in VS Code IDEUmbrage
I think this is a hack, though this solved the problem but Is there any valid and permanent solution for this problem?Hyperaemia
I think this is the best solution to this problem, I don't think it's viable to npm i every package that throws the errThuthucydides
Work perfect on VSC with react using typescriptTolle
This is a very bad practice, as the any type just ignores Typescript linting. So what's the point? It's a step back toward normal javascript, instead of using Typescript. For me the following answer solved it, and is considered much better: #41463229Konyn
Problem solving begins from a simplest answer to avoid blockers during development. Next step is to find more elegant and focussed solutionHarl
F
61

Make sure to stop your react local server and start it again after doing the following:

1- Create .d.ts file manually, you just need to do the following:

2 - enter src folder

3 - create global.d.ts file

4 - declare modules in it like:

declare module 'module-name';

I answered it here before and it was nice

Faceless answered 8/1, 2021 at 12:43 Comment(3)
Thanks! This works the best out of all the solutions mentioned hereKremlin
Make sure to stop your react local server and start it again if it still doesn't work after doing the above. npm startArmes
This helped alongside the response hereZloty
P
60

If there is no @types/<package> for the module you are using, you may easily circumvent the issue by adding a // @ts-ignore comment above i.e.

// @ts-ignore
import { Navbar, NavItem } from 'react-materialize';

Alternatively you may create the missing @types/<package> following:

declaration files publishing

DefinitelyTyped how can i contribute

Portiere answered 3/7, 2019 at 20:53 Comment(2)
This package doesn't have a @types available yet BTW. Created an issue on the repo... github.com/geelen/react-snapshot/issues/131Thoughtout
In 2022: Do not use "@ts-ignore" because it alters compilation errorsRadley
M
35

In my case I had a problem with react, so I started doing:

npm install @types/react

and then to the

npm install @types/react-dom

This worked for me

Misconception answered 4/6, 2020 at 12:26 Comment(3)
Even tough they were already installed, re-running those commands solved the problem for me.Hrutkay
Basically, it updates your existing packages when you re-run the commands. That was the reason the problem is solved.Consalve
npm install @types/react was enough in my case. Thx!Konyn
R
27

Just install the necessary types for react and it should solve the error.

if you are using yarn:

yarn add @types/react @types/react-dom @types/react-router-dom -D

if you are using npm:

npm install @types/react @types/react-dom @types/react-router-dom --save-dev
Reformed answered 19/1, 2021 at 19:34 Comment(1)
Thanks. This worked for me. Surprising that I had to do this as I had created the react app using the command npx create-react-app react-quiz --template typescript I thought this would be enough to take care of all dependenciesEmpress
C
8

I had this same problem but not necessarily relating to typescript, so I struggled a bit with these different options. I am making a very basic create-react-app using a specific module react-portal-tooltip, getting similar error:

Could not find a declaration file for module 'react-portal-tooltip'. '/node_modules/react-portal-tooltip/lib/index.js' implicitly has an 'any' type. Try npm install @types/react-portal-tooltip if it exists or add a new declaration (.d.ts) file containing declare module 'react-portal-tooltip';ts(7016)

I tried many steps first - adding various .d.ts files, various npm installs.

But what eventually worked for me was touch src/declare_modules.d.ts then in src/declare_modules.d.ts:

declare module "react-portal-tooltip";

and in src/App.js:

import ToolTip from 'react-portal-tooltip';
// import './declare_modules.d.ts'

I struggled a bit with the different locations to use this general 'declare module' strategy (I am very much a beginner) so I think this will work with different options but I am included paths for what worked work me.

I initially thought import './declare_modules.d.ts' was necessary. Although now it seems like it isn't! But I am including the step in case it helps someone.

This is my first stackoverflow answer so I apologize for the scattered process here and hope it was still helpful! :)

Conservative answered 9/4, 2020 at 18:35 Comment(1)
This is really neat and simple. Thank you. Keep up the good work!Sergio
G
7

All you need to do is run the below script. Then, remove/re-install the module that you want to use.

npm install --save @types/react-redux
Globefish answered 8/3, 2018 at 14:59 Comment(1)
There doesn't appear to be a @types/react-materialize availableSpruce
I
7

Had the same error with react-router-dom.

This error occurs when you're working on a typescript project, default modules doesn't come with types that are needed with typescript.

So I searched on npmjs.com a package named @types/react-router-dom if it's not react-router-dom you're seeking for then replace the react-router-dom by your untyped module.

Seek for the last version on the npm website, then add this in your package.json like so :

[...]
"dependencies": {
    [...]
    "@types/react-router-dom": "^5.1.8",
    [...]
  },
[...]

Once it's done, run yarn install

Then it should rocks !

Inseparable answered 13/7, 2021 at 14:18 Comment(2)
You can also add it in "devDependencies": "devDependencies": { "@types/react-router-dom": "^5.1.8" }Conglutinate
Note: Add types module to devDependencisesOlds
M
6

I had this issue when I added react-router-dom to the new CRA app using typescript. After I added @types/react-router, the issue was fixed.

Mcloughlin answered 1/11, 2019 at 16:29 Comment(1)
What to do after installing this?Hyperaemia
C
5

A more hacky way is to add eg., in boot.tsx the line

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

with

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

in declare_modules.d.ts

It works but other solutions are better IMO.

Chastise answered 6/1, 2018 at 22:17 Comment(0)
Y
5

Could not find a declaration file for module 'react/jsx-runtime'

If someone's having this error, then you'll be surprised to know that when creating react app with create-react-app, it only shows @types/react in package.json. But if you check inside the node_modules/@types folder, you'll not find any folder of react.

Solution is to just wipe out the node_modules folder completely and reinstall - npm install or maybe can go with the individual module installation - npm install @types/react.

Yongyoni answered 21/12, 2020 at 10:47 Comment(3)
The most relevant answer that completely solves the problem, thanks!Necktie
Cool! Tnx! npm install -D @types/react IMO would be betterLunna
@Lunna yeah but who knows if in future just like @types/react some other module is also absent then we can go with npm installYongyoni
I
4

Also, this error gets fixed if the package you're trying to use has it's own type file(s) and it's listed it in the package.json typings attribute

Like so:

{
  "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
}
Involucel answered 18/12, 2017 at 9:13 Comment(0)
T
3

What I did was run the following commands from nodejs command prompt while in the project folder directory:

  1. npm init
  2. npm install -g webpack
  3. npm install --save react react-dom @types/react @types/react-dom
  4. npm install --save-dev typescript awesome-typescript-loader source-map-loader
  5. npm install ajv@^6.0.0
  6. npm i react-html-id
  7. import the package(in node modules) in App.js file by adding the code: import UniqueId from 'react-html-id';

I did the above(although I already had npm installed) and it worked!

Tierell answered 25/10, 2018 at 7:39 Comment(0)
L
3

I got it after lots of trouble In react app there will be a react-app-env.d.ts file in src folder just declare module there

/// <reference types="react-scripts" />
declare module 'react-color/lib/components/alpha/AlphaPointer'

Loudhailer answered 24/12, 2020 at 8:5 Comment(0)
G
3

Created a file called index.d.ts in the src folder and added

declare module 'react-materialize';

then included the file in tsconfig.json file like:

 "include": [
   "src",
   "index.d.ts"
 ]
Gurdwara answered 15/2, 2023 at 4:13 Comment(0)
R
1

These are notes if you want to just declare types for a node module where typings are not available.

Solution--->

  1. create a global.d.ts file inside src directory.
  2. because typescript is already watching src directory
  3. and thus we avoid explicitly stating in tsconfig.json to load particular type files also, because in doing so we other default typing location can get overriden
Rheumatism answered 28/9, 2021 at 10:23 Comment(0)
C
1

I've had the same problem, The simplest solution was

npm i --save-dev @types/react-router && npm i --save-dev @types/react-router-dom

Caper answered 18/10, 2022 at 12:7 Comment(0)
V
0

For my case the issue was that the types were not getting added to package.json file under devDependencies to fix it I ran npm install --save-dev @types/react-redux note the --save-dev

Vitalize answered 10/1, 2019 at 22:8 Comment(0)
M
0

try adding to tsconfig.json file: "noImplicitAny": false worked for me

Marelya answered 27/8, 2020 at 1:44 Comment(1)
You're basically disabling all of the goodness provided by TypeScript by doing this. Why not just use regular JS?Philbert
K
0

In case if you have problem with react npm install --save @types/reactnpm install --save @types/react

Kamila answered 13/9, 2021 at 5:31 Comment(0)
P
0

Like this one:

{
  "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
}

Also, this error gets fixed if the package you're trying to use has it's own type file(s) and it's listed it in the package.json typings attribute

Purveyor answered 28/9, 2021 at 4:42 Comment(1)
do not understand your solution, what are you suggesting?Rheumatism
R
0

Try npm i --save-dev @types/react-materialize if it exists or add a new declaration (.d.ts) file containing declare module 'react-materialize';

Reproval answered 7/11, 2021 at 1:31 Comment(0)
I
0

You can find a proper solution over here. You just need to replace react-items-carousel with react-materialize.

Indusium answered 12/1, 2022 at 19:29 Comment(0)
H
-2

works just fine

npm install @types/react-materialize
Hearttoheart answered 21/6, 2019 at 5:58 Comment(1)
there are no types in for react-materializeResign
C
-2

There might be cases when some dependencies doest not have types and ts forces you to include types for that. There is a workaround to resolve the issue, which is to use javascript instead.

e.g.

import {Charts} from "react-charts"; //error: Could not find a declaration file for module 'react-charts'.

Workaround: const ReactChart = require("react-charts"); //no-error

Carnage answered 30/1, 2021 at 13:44 Comment(1)
you can write js in a ts file. It won't show you any errors. It is compiled to javascript only in the end after all.Carnage

© 2022 - 2024 — McMap. All rights reserved.