Overview
I have created a common react library to be used within a range of applications. I had no issues with it prior to upgrading on of the applications to React 18.
I am using rollup to do the library configuration and the library also has a documentation site.
Issue
When I download the package from artifacts it seems fine and works but when I install the package it seems to create a node_modules folder within the library and install an older version of React.
This causes a React version mismatch error and stops the app from running until I manually delete the node_modules folder, it then runs correctly without issue.
Additionally this behaviour doesn't happen in a different application we have which is running React 17.0.2, it seems to only have become an issue in our React 18 apps.
Example
Here you can see that the package has it's own version of React installed which doesn't exist when I download the package directly from artifacts.
Rollup configuration
The library is running React 18 and so is the application and we use Rollup to do the library configuration.
export default {
input: './src/lib/index.ts',
output: [
{
file: packageJson.main,
format: 'cjs',
sourcemap: true,
preserveModules: false
},
{
file: packageJson.module,
format: 'esm',
sourcemap: true,
preserveModules: false
}
],
plugins: [
peerDepsExternal(),
commonjs(),
typescript({
tsconfigOverride: {
exclude: [
'**/__tests__',
'**/*.test.ts',
'**/*.test.tsx',
'./src/tests',
'./src/examples',
'**/*.example.ts',
'**/*.example.tsx',
'**/ApplicationRouter.tsx',
'**/index.tsx'
],
include: ['src/lib']
}
}),
postcss({
extract: true,
minimize: true,
use: [
['sass', {
includePaths: [
'./src/lib',
'./node_modules'
]
}]
]
})
]
};
I think it's the npm version causing the problem. If you are using npm >=v7 try to install the dependencies with
npm i --legacy-peer-deps
And see if that solves the problem.
Also, I see you are creating a React Component Library, there's a tool Create React Package lets you do this with Rollup, Babel, ESLint, TypeScript, and PostCSS already set up. So you can focus on the code rather than configuration.
I have a React app that I created using npx create-react-app my-app. I've been regularly updating both React and other npm packages.
A while ago, I started getting the following warning:
#babel/polyfill is deprecated. Please, use required parts of core-js
and regenerator-runtime/runtime separately
The following is what's in my package.json file:
"devDependencies": {
"babel-polyfill": "^6.26.0",
"redux-devtools": "^3.5.0"
}
I found a few articles online about how to handle this but none of them look like the official solution. What's the right way to handle this?
So far, this has been a warning and not an error so I just postponed dealing with it. Today, I upgraded the moment package and that started giving me an error and I figured dealing with this issue first is a good starting point.
I'd appreciate some pointers in making this warning go away.
babel-polyfill is being replaced by core-js. You can remove babel-polyfill and install core-js instead. After you have installed core-js update the babel presets in your .babelrc or babel.config.js file with the following:
"presets":[
['#babel/preset-env',
{
useBuiltIns: 'usage',
corejs: 3,
}],
]
If you are importing babel-polyfill in your App you can remove that too. Also you can add a targets property in your presets
[
'#babel/preset-env',
{
targets: {
browsers: ['> 0.25%, not dead'],
},
useBuiltIns: 'usage',
corejs: 3,
},
]
Since I reinstalled my NPM dependencies in my create-react-app project, Atom's eslint gives me this error on the top of every file :
Error while running ESLint: Cannot find module 'eslint-config-react-app'.
While react-scripts's eslint doesn't raise any warning or error. By the way, the package eslint-config-react-app is installed in node_modules.
I tried to reinstall linter-eslint, atom, the NPM dependencies, etc. Nothing worked.
Has anyone an idea ?
Here is my .eslintrc :
{
"extends": "react-app",
"rules": {
"jsx-a11y/anchor-is-valid": 0,
"react/jsx-no-comment-textnodes": 0,
"no-unused-vars": ["warn", {
args: "all",
ignoreRestSiblings: false,
argsIgnorePattern: "^_$"
}]
}
}
Edit : I don't know why, but all of the sudden, the error changed and now it's this one on top of every js file :
Error while running ESLint: Invalid regular expression flags
EDIT 2
None of the given solutions worked in my case. The problem with linter-eslint is not solved. But I found a workaround for now : using fast-eslint instead of linter-eslint. Works just fine.
For anyone else trying this, one solution is installing eslint-config-react-app globally, along with all of its peer deps. At the moment, that's:
Refer to https://github.com/facebook/create-react-app/issues/3633#issuecomment-361485857
npm i -g babel-eslint#^7.2.3 eslint#^4.1.1 eslint-plugin-flowtype#^2.34.1 eslint-plugin-import#^2.6.0 eslint-plugin-jsx-a11y#^5.1.1 eslint-plugin-react#^7.1.0
Since you mentioned that the problem occurred after you reinstalled
npm modules, it might be related to npm dependency resolution. So
you might have multiple versions of the same module and atom might
use the wrong one.
Check whether your react-scripts version is 0.2.0 or higher according to Displaying Lint Output in the Editor
Remove all eslint related dependencies from your package.json.
This means removing babel-eslint, eslint, eslint-plugin-flowtype,
eslint-plugin-import, eslint-plugin-jsx-a11y, eslint-plugin-react, etc
Create react app installs all the linting dependencies under the
hood so you shouldn't manually install them.
Remove all global eslint related dependencies.
Generally you don't need to use globally installed eslint dependencies as you might
have different eslint versions in each of your projects.
The latest version of linter-eslint should then use the correct eslint dependencies from under node_modules in your project.
UPDATE: Based on your comments and edited question, I tried reproducing this using CRA, replacing the dependencies in package.json, reinstalling and adding the following .eslintrc.
Notice I added " around the options keys in the "no-unused-vars".
{
"extends": "react-app",
"rules": {
"jsx-a11y/anchor-is-valid": 0,
"react/jsx-no-comment-textnodes": 0,
"no-unused-vars": ["warn", {
"args": "all",
"ignoreRestSiblings": false,
"argsIgnorePattern": "^_$"
}]
}
}
Having done all this I couldn't reproduce the issue.
I built react project with webpack and babel.
It worked well.
But, today I got some error below.
ERROR in ./index.js
Module build failed (from ./node_modules/babel-loader/lib/index.js):
Error: [BABEL] /home/rpf5573/react-discovery-v2/src/admin/admin-client/index.js: Invalid Option: corejs is not a valid top-level option.
Maybe you meant to use 'targets'? (While processing: "/home/rpf5573/react-discovery-v2/src/admin/admin-client/node_modules/#babel/preset-env/lib/index.js")
at validateTopLevelOptions (/home/rpf5573/react-discovery-v2/src/admin/admin-client/node_modules/#babel/preset-env/lib/normalize-options.js:49:13)
at normalizeOptions (/home/rpf5573/react-discovery-v2/src/admin/admin-client/node_modules/#babel/preset-env/lib/normalize-options.js:160:3)
at _default (/home/rpf5573/react-discovery-v2/src/admin/admin-client/node_modules/#babel/preset-env/lib/index.js:168:37)
....
error Command failed with exit code 2.
And this is my admin-client/.babelrc
module.exports = {
compact: true,
presets: [
[
"#babel/preset-env",
{
modules: false,
targets: {
browsers: ["since 2015"]
},
useBuiltIns: "usage",
corejs: "2"
}
],
"#babel/preset-react"
],
plugins: [
"#babel/plugin-proposal-class-properties"
]
}
What did I wrong ?
What should I do ?
Maybe you had some major version change recently?
In my case, that was the cause. And the solution was:
Completely remove node-modules folder from the project
Remove package-lock.json file from the project root folder
Perform a npm i to recreate everything
As an alternative to entirely deleting node-modules as suggested by #SysDragon. I looked deeper into why the complaint was specific to corejs. And i found a great remedy here.
In summary, these were the steps taken:
Install the latest version of core-js (or atleast version-3).
Whatever you are building "might" be making use of latest JavaScript functionality. For my case, i was working with react-16.9.0 and mdbreact-4.19.0.
Note: Core-js is a Modular standard library for JavaScript. Includes polyfills for ECMAScript up to 2019: promises, symbols, collections, iterators, typed arrays and many other features.
Then, you need to update your transpiler, babel and it's "relatives".
This block snippet helped accomplish that: yarn upgrade #babel/core #babel/plugin-transform-runtime #babel/polyfill #babel/preset-env #babel/runtime babel-loader --dev.
The --dev part is just to save them as devDependencies (if using yarn) (for npm, use --save-dev), which i advocate for especially if this is team project.
In my case, the subdirectory I was working on was part of a larger Yarn workspace in the root of the repository and removing that subdirectory from the workspace and reinstalling node_modules within the subdirectory fixed the issue.
This issue is most likely caused by dependency mismatches.
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
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.
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 :)
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"]
}
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
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
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
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
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! :)
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
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 !
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.
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.
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.
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
}
What I did was run the following commands from nodejs command prompt while in the project folder directory:
npm init
npm install -g webpack
npm install --save react react-dom #types/react #types/react-dom
npm install --save-dev typescript awesome-typescript-loader source-map-loader
npm install ajv#^6.0.0
npm i react-html-id
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!
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'
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
try adding to tsconfig.json file: "noImplicitAny": false
worked for me
In case if you have problem with react npm install --save #types/reactnpm install --save #types/react
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
These are notes if you want to just declare types for a node module where typings are not available.
Solution--->
create a global.d.ts file inside src directory.
because typescript is already watching src directory
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
Try npm i --save-dev #types/react-materialize if it exists or add a new declaration (.d.ts) file containing declare module 'react-materialize';
You can find a proper solution over here. You just need to replace react-items-carousel with react-materialize.
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
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"
]
works just fine
npm install #types/react-materialize
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