Atom & eslint: Cannot find module 'eslint-config-react-app' - reactjs

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.

Related

Error Could not resolve entry module React + Rollup

I need to build shareable React component which could be used across apps.
For this, I was/am following the below article
https://dev.to/alexeagleson/how-to-create-and-publish-a-react-component-library
My Configuration looks exactly the same except the npm packages version (even tried with the same versions)
The folder structure looks the same as below
rollup.config.js
import resolve from "#rollup/plugin-node-resolve";
import commonjs from "#rollup/plugin-commonjs";
import typescript from "#rollup/plugin-typescript";
import dts from "rollup-plugin-dts";
const packageJson = require("./package.json");
export default [
{
input: "src/index.ts",
output: [
{
file: packageJson.main,
format: "cjs",
sourcemap: true,
},
{
file: packageJson.module,
format: "esm",
sourcemap: true,
},
],
plugins: [resolve(), commonjs(), typescript({ tsconfig: "./tsconfig.json" })],
},
{
input: "dist/esm/types/index.d.ts",
output: [{ file: "dist/index.d.ts", format: "esm" }],
plugins: [dts()],
},
];
npm script
"rollup": "rollup -c"
However when I run npm run rollup this throws the below error
[!] Error: Could not resolve entry module (dist/esm/types/index.d.ts).
Error: Could not resolve entry module (dist/esm/types/index.d.ts)
Please suggest. Thanks!
I also ran into the same problem you are experiencing when working with rollup. After spending some while digging for the solution, I finally got to solve this problem.
My Configuration looks exactly the same except the npm packages version (even tried with the same versions)
The exception you have stated is actually the problem. The problem lies in package versioning. The package #rollup/plugin-typescript versions later than 8.3.3 are not generating nor storing the declaration files in the types folder expected to be at the path: dist/cjs/ and dist/esm/.
The latest version at this point in time is 8.5.0 which still breaks. Hopefully it is fixed in near future.
Steps to fix your error
Make sure your tsconfig.json file has "declarationDir": "types" to direct the bundler's typescript plugin to create and store declaration files in the types folder when you run npm run rollup
Uninstall the existing #rollup/plugin-typescript package version by running npm un #rollup/plugin-typescript --save-dev
Install #rollup/plugin-typescript with the command npm i #rollup/plugin-typescript#8.3.3 --save-dev. As you see, we are picking a specific version.
If you still encounter problems:
Manually update the package.json file like: "#rollup/plugin-typescript": "8.3.3". Note that I have removed the caret(^) symbol to tie the package to version 8.3.3.
Delete the node_modules folder. You could use the command rm -rf node_modules.
Delete package-lock.json.
Run npm i to install the packages again with versions specified in package.json
Here's an working answer for people coming from 2023 that doesn't lock you to an outdated version of #rollup/plugin-typescript:
Preconditions: Make sure that you get rid off your package-lock.json and your node_modules directory so that you can start from a clean slate and install your project again.
run npm install tslib --save-dev
add "type": "module" to package.json
in tsconfig.json, add "rootDir": "src"
in rollup.config.js, change plugins: [dts()] to plugins: [dts.default()]
back in package.json, add --bundleConfigAsCjs as a parameter to the rollup command in scripts
After that you should be able to continue with the tutorial and be able to create a new build via npm run rollup.
I fixed the error of 'Could not resolve entry module (dist/esm/index.d.ts)'.
I tried removing types, downgrading react to match the version in the tutorial but none worked.
I found this comment on the tutorial which was helpful: https://dev.to/nasheomirro/comment/239nj
I got rid of main and common js set up in both rollup config and package json.
i changed the packagejson variable to import packageJson from "./package.json" assert { type: "json" };
added types back into the input in the rollup config
Set "#rollup/plugin-typescript" to be version "8.3.3" as mentioned above.
I now have a Dist folder with an ESM folder and didn't get any errors.

Why is create-react-app giving typescript error when trying to eslint (though no typescript files)?

I am getting this error when i do eslint .
Error: Failed to load parser '#typescript-eslint/parser' declared in '.eslintrc.yml ยป
eslint-config-react-app#overrides[0]': Cannot find module 'typescript'
Because there are TypeScript files - although ESLint excludes node_modules/ from linting by default, CRA's override (in order to load appropriate plugins for any TypeScript files, this is why you see eslint-config-react-app#overrides[0] in the output) does include the content of node_modules/ (see e.g. this issue).
You can install TypeScript, but if you don't intend to use any TypeScript files in your own source you can be more specific about what you want to lint instead. Either:
Change the script in package.json to only lint the source directory:
"lint": "eslint src/"
Create a .eslintignore (or another file, if you set the right --ignore-path) file containing:
node_modules/
Add to the ESLint configuration object in package.json (or pass it as an --ignore-pattern):
"eslintConfig": {
"extends": "react-app",
"ignorePatterns": [
"node_modules/"
]
}
The latter two options are from the documentation on configuration.
Install typescript
npm install --save-dev typescript
Then eslint .
will work as intended and can use the .eslintrc.yml file.
btw I had also just installed
npm install -save-save eslint-plugin-typescript
in case you are doing this as you may need that too.
btw do NOT install eslint with npm install eslint... as it may mess up due to the fact that... crewate-react-app already has it (but a lower version). so just add your eslintrc.yml file and it will work (be applied in the editor, and with this fix, at the command line as neeeded).
example output:
.../src/components/SignUp/index.js
1:41 error Missing semicolon semi
7:26 error Unnecessary parentheses around expression no-extra-parens
15:2 error Missing semicolon semi
32:13 error 'username' is assigned a value but never used no-unused-vars
47:63 error Missing semicolon semi
95:26 error Unnecessary parentheses around expression no-extra-parens

React Native Start failing to compile because of linter errors

I recently upgraded RN from version 0.59.4 to 0.61.5.
I want to use react-native-web and added react-scripts (3.4.1) and react-dom (16.13.1). After some cleaning up of native modules not supported, running react-scripts start results in failure to compile because of lint errors (no-undef, for example). These are not rules set up in my .eslintrc.js file and seem to come with the new way of running scripts.
Is there a way to ignore the lint errors to try and compile? While I could try and make these changes in my code base, there are some issues in dependencies as well.
Additionally, I updated my babel as per react-native-web
module.exports = {
presets: ['module:metro-react-native-babel-preset'],
plugins: [
["module-resolver", {
"alias": {
"^react-native$": "react-native-web"
}
}]
]
};
I have read this might be webpack related, but there is currently no webconfig. I tried adding what was on the react-native-web getting starter page, but had no luck there either.

babel/polyfill is deprecated warning in create-react-app

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,
},
]

Invalid Option: corejs is not a valid top-level option

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.

Resources