Eslint error causing create-react-app failed to compile - reactjs

I am building a website using create-react-app and have just installed eslint to it.
For some reason what was supposed to be shown as eslint warnings are showing up as errors and causing npm run start to fail.
How can I bypass this issue and have them shown as warnings like before ?
My .eslintrc.js
env: {
browser: true,
es6: true,
jest: true,
},
extends: [
'airbnb-typescript',
'plugin:#typescript-eslint/recommended',
'prettier/react',
'prettier/#typescript-eslint',
'plugin:prettier/recommended',
],
globals: {
Atomics: 'readonly',
SharedArrayBuffer: 'readonly',
},
parser: '#typescript-eslint/parser',
parserOptions: {
ecmaFeatures: {
jsx: true,
},
ecmaVersion: 2018,
sourceType: 'module',
project: './tsconfig.json',
},
plugins: ['react', '#typescript-eslint'],
rules: {
'class-methods-use-this': 'off',
'additional-rule': 'warn',
},
ignorePatterns: ['**/node_modules/*', '**/build/*', 'config-overrides.js'],
};
My package.json
"name": "device-protection-renewal-web",
"version": "0.1.0",
"private": true,
"dependencies": {
"#testing-library/jest-dom": "^5.11.5",
"#testing-library/react": "^11.1.0",
"#testing-library/user-event": "^12.1.10",
"#types/jest": "^26.0.15",
"#types/node": "^12.19.3",
"#types/react": "^16.9.55",
"#types/react-dom": "^16.9.9",
"babel-polyfill": "^6.26.0",
"core-js": "^3.6.5",
"i18next": "^19.8.3",
"react": "^17.0.1",
"react-app-polyfill": "^2.0.0",
"react-dom": "^17.0.1",
"react-i18next": "^11.7.3",
"react-scripts": "4.0.0",
"web-vitals": "^0.2.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all",
"ie >= 9"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version",
"ie >= 9"
]
},
"devDependencies": {
"#babel/plugin-transform-arrow-functions": "^7.12.1",
"#typescript-eslint/eslint-plugin": "^4.6.1",
"#typescript-eslint/parser": "^4.6.1",
"eslint": "^7.11.0",
"eslint-config-airbnb-typescript": "^9.0.0",
"eslint-config-prettier": "^6.11.0",
"eslint-plugin-import": "^2.22.0",
"eslint-plugin-jsx-a11y": "^6.3.1",
"eslint-plugin-prettier": "^3.1.4",
"eslint-plugin-react": "^7.20.6",
"eslint-plugin-react-hooks": "^4.1.0",
"prettier": "^2.1.2",
"typescript": "^4.0.5"
}
}```
[1]: https://i.stack.imgur.com/WUKcz.png

I assume that you have installed ESLint using npm install eslint --save-dev and defined a default configuration with node_modules/.bin/eslint --init answering the questions in the prompt.
I noticed that in your .eslintrc.js file, the ESLint settings is missing in the extends option:
extends: [
'eslint:recommended',
'airbnb-typescript',
'plugin:#typescript-eslint/recommended',
'prettier/react',
'prettier/#typescript-eslint',
'plugin:prettier/recommended',
],
Also in the package.json is recommended ESLint to have its own script that you run using npm run lint and use it combined with a eslint-plugin in your favorite code editor:
{
"scripts": {
"start": "react-scripts start",
// ...
"lint": "eslint ."
},
}
Probably you will build your application at some point, so you should create a .eslintignore file and inside of it add build since files in the build directory also get checked by default when the command is ran.
Source: https://fullstackopen.com/en/part3/validation_and_es_lint#lint

This part in your package.json is unnecessary; since you have an aslant config file, it should be moved to .eslintrc.js.
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
}
Which would then turn into this:
extends: [
'react-app',
'react-app/jest',
'airbnb-typescript',
'plugin:#typescript-eslint/recommended',
'prettier/react',
'prettier/#typescript-eslint',
'plugin:prettier/recommended'
]
However; in the latest versions of the eslint-config-react-app plugin at this time (7.31.11), I'm getting a jest plugin conflict error with a project that worked prior to updating unless I remove react-app/jest from my eslint configs extends section.
Which is how I ended up here, currently trying to find out what caused this.
Update: So my issue was caused because eslint-config-react-app depends on eslint-plugin-jest^25.7.0 and I was using the latest eslint-plugin-jest^27.1.6. I removed my package.json's dependency and will use the version included with eslint-config-react-app so there aren't any conflicts there, but if I need features of the newer plugin version, a quick npm i -D on it, changing eslint configuration from automatic to manual and specifying the path to the local node_modules version and .eslintrc.js should work as well; aside from any conflicts with the config plugin.

Related

TailwindCSS classes arent showing up (React) when deployed via Netlify, where did I go wrong?

Hi Ive been racking my brain on what Ive been doing wrong. Locally the formatting is correct, but when I push it to Netlify, Tailwindcss decides to not work. Am I purging wrong? I have a tailwind css file in my dist and src folders.
package.json
{
"name": "bgg_version_3",
"keywords": [
"design, graphic design, art, illustration, web design, Virginia, Massachusetts, Boston, Newport News"
],
"homepage": ".",
"version": "0.1.0",
"private": true,
"dependencies": {
"#testing-library/jest-dom": "^5.14.1",
"#testing-library/react": "^11.2.7",
"#testing-library/user-event": "^12.8.3",
"bootstrap": "^5.0.2",
"framer-motion": "^4.1.17",
"isomorphic-unfetch": "^3.1.0",
"next": "^11.0.1",
"react": "^17.0.2",
"react-bootstrap": "^2.0.0-beta.4",
"react-dom": "^17.0.2",
"react-helmet": "^6.1.0",
"react-router": "^5.2.0",
"react-router-dom": "^5.2.0",
"react-scripts": "^4.0.3",
"react-tsparticles": "^1.33.2",
"react-visibility-sensor": "^5.1.1",
"web-vitals": "^1.1.2"
},
"scripts": {
"build:tailwind": "tailwindcss build -i src/tailwind.css -o src/tailwind.output.css",
"prestart": "npm run build:tailwind",
"prebuild": "npm run build:tailwind",
"start": "craco start",
"build": "react-scripts build",
"test": "craco test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"sass": "^1.36.0"
}
}
tailwind.config.js
module.exports = {
purge: ["./src/*.{js,jsx,ts,tsx}", './src/*.{scss,css}',"./src/components/*.{js,jsx,ts,tsx}","./src/components/*.{scss,css}"],
darkMode: false, // or 'media' or 'class'
theme: {
extend: {},
},
variants: {
extend: {},
},
plugins: [],
}
Any clue as to why it wont seem to work when deployed or what I did wrong?
Normally the devDependecies in your package.json would look somthing like this when using tailwindcss:
"devDependencies": {
"autoprefixer": "^10.3.1",
"postcss": "^8.3.6",
"tailwindcss": "^2.2.7"
}
So I would suggest you redo the process of installing tailwindcss and remember the --save flag on every install

How to disable prettier errors in reactjs project and also no-unsued-vars error

I keep getting these errors when i am trying to run a reactjs project
prettier/prettier
Line 23:39: Replace `'react-toastify'` with `"react-toastify"` prettier/prettier
Line 29:59: Delete `⏎` prettier/prettier
And following is my .eslintrc.js
module.exports = {
parser: "babel-eslint",
env: {
es6: true,
node: true,
browser: true,
},
parserOptions: {
ecmaVersion: 6,
sourceType: "module",
ecmaFeatures: {
jsx: true,
},
},
plugins: ["react"],
extends: [
"eslint:recommended",
"plugin:react/recommended",
"plugin:prettier/recommended",
]
};
and following is the package.json
{
"name": "material-dashboard-react",
"version": "1.10.0",
"description": "Material Dashboard React. Coded by Creative Tim",
"private": false,
"main": "dist/index.js",
"dependencies": {
"#material-ui/core": "4.11.4",
"#material-ui/icons": "4.11.2",
"axios": "^0.21.1",
"chartist": "0.10.1",
"classnames": "2.3.1",
"history": "5.0.0",
"perfect-scrollbar": "1.5.1",
"prop-types": "15.7.2",
"react": "17.0.2",
"react-chartist": "0.14.4",
"react-dom": "17.0.2",
"react-router-dom": "5.2.0",
"react-scripts": "4.0.3",
"react-swipeable-views": "0.14.0",
"react-toastify": "^7.0.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject",
"install:clean": "rm -rf node_modules/ && rm -rf package-lock.json && npm install && npm start",
"lint:check": "eslint . --ext=js,jsx; exit 0",
"lint:fix": "eslint . --ext=js,jsx --fix; exit 0",
"build-package-css": "cp src/assets/css/material-dashboard-react.css dist/material-dashboard-react.css",
"build-package": "npm run build-package-css && babel src --out-dir dist"
},
"repository": {
"type": "git",
"url": "git+https://github.com/creativetimofficial/material-dashboard-react.git"
},
"keywords": [],
"author": "Creative Tim <hello#creative-tim.com> (https://www.creative-tim.com/)",
"license": "MIT",
"bugs": {
"url": "https://github.com/creativetimofficial/material-dashboard-react/issues"
},
"homepage": "https://creativetimofficial.github.io/material-dashboard-react/#/dashboard",
"optionalDependencies": {
"#babel/core": "7.14.0",
"typescript": "4.2.4"
},
"devDependencies": {
"eslint-config-prettier": "8.3.0",
"eslint-plugin-prettier": "3.4.0",
"gulp": "4.0.2",
"gulp-append-prepend": "1.0.9",
"prettier": "2.2.1"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": []
}
}
If you don't want prettier errors in your eslint output, just remove that
"plugin:prettier/recommended",
line in the Eslint configuration.
However, it would just be better to adhere to those suggestions -- depending on your IDE, you can just tell it to run eslint --fix for you, which'll fix that right up.
If you do not want it to completely stop you from running the web app, you can change the severity level of prettier from "error" to "warn".
in your .eslint.json file rules object, change from
"prettier/prettier": "error",
to
"prettier/prettier": "warn",

react-scripts jest 27 version dependency error

I wanted to start testing my React component with frameworks jest and enzyme. I'm writing a code with Typescript.
So I installed packages like: jest, enzyme, enzyme-adapter-react-16, ts-jest and typescript types required dependencies. Below I show entire package.json file:
{
"name": "test-learning-with-redux",
"version": "0.1.0",
"private": true,
"dependencies": {
"#testing-library/jest-dom": "^5.14.1",
"#testing-library/react": "^11.2.7",
"#testing-library/user-event": "^12.8.3",
"#types/node": "^12.20.16",
"#types/react": "^17.0.14",
"#types/react-dom": "^17.0.9",
"node-sass": "^6.0.1",
"react": "^16.14.0",
"react-dom": "^16.14.0",
"react-scripts": "4.0.3",
"web-vitals": "^1.1.2"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "jest --watch",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"#types/enzyme": "^3.10.9",
"#types/enzyme-adapter-react-16": "^1.0.6",
"#types/jest": "^26.0.24",
"enzyme": "^3.11.0",
"enzyme-adapter-react-16": "^1.15.6",
"enzyme-to-json": "^3.6.2",
"jest": "^27.0.6",
"jest-enzyme": "^7.1.2",
"ts-jest": "^27.0.3",
"typescript": "^4.3.5"
}
}
The problem is that after installation dependencies tests works fine but react server dosen't work. I can observe error like on the screen below:
Of course I can solve this issue by adding .env with: SKIP_PREFLIGHT_CHECK=true
and it works, but I'm not for sure if it's the best solution.
On the other hand if I delete dependency with jest by typing npm uinstall jest, jest won't work and there will be an error: TypeError: Jest: a transform must export a process function.
More on the screen below:
Below I show jest.config.js configuration file:
My module.exports = {
"roots": [
"<rootDir>/src"
],
"transform": {
"^.+\\.tsx?$": "ts-jest"
},
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$",
"moduleFileExtensions": [
"ts",
"tsx",
"js",
"jsx",
"json",
"node"
],
"snapshotSerializers": ["enzyme-to-json/serializer"],
"setupFilesAfterEnv": ["<rootDir>/src/setupEnzyme.ts"],
}
What's the best way to solve this problem in order to work normally with jest and typescript simultaneously? I could observe some comments where people write about forcing never version by react-scripts but I don't know how to do it. Maybe it's is better to stay with version from react-scripts and somehow solve TypeError from jest mentioned by me above but how?
I would be greateful for advices, prepositions.

Eslint import/no-extraneous-dependencies error on path

I am building a React app with Eslint setup.
In my app, I am using GraphQL #apollo/client dependency.
When I tried to do import { setContext } from '#apollo/client/link/context'
I got an eslint error of
'#apollo/client/link/context' should be listed in the project's dependencies. Run 'npm i -S #apollo/client/link/context' to add it import/no-extraneous-dependencies
I do have '#apollo/client' dependency in my package.json.
And import from '#apollo/client/link/context' is the proper way to get 'setContext' according to Apollo documentation.
Seems like import/no-extreaneous-dependencies is not recognize the nested path from '#apollo/client'.
When I set "import/no-extraneous-dependencies": 0 in my .eslintrc.js rules, it will work fine.
But for a proper solution, instead of just turning off the eslint checking, I am assuming that I probably need to set something up with .eslintrc.js rules.
What other set ups should I write for my rules in my .eslintrc.js in this case for properly solving the problem?
my package.json:
{
"name": "my-app",
"version": "0.1.0",
"private": true,
"dependencies": {
"#apollo/client": "^3.3.19",
"#auth0/auth0-react": "^1.4.0",
"#testing-library/jest-dom": "^5.11.4",
"#testing-library/react": "^11.1.0",
"#testing-library/user-event": "^12.1.10",
"eslint": "^7.26.0",
"eslint-config-airbnb": "^18.2.1",
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-jsx-a11y": "^6.4.1",
"eslint-plugin-react": "^7.23.2",
"eslint-plugin-react-hooks": "^4.2.0",
"graphql": "^15.5.0",
"react": "^17.0.2",
"react-big-calendar": "^0.33.3",
"react-dom": "^17.0.2",
"react-router-dom": "^5.2.0",
"react-scripts": "4.0.3",
"web-vitals": "^1.0.1"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {}
}
and my .eslintrc.js file:
module.exports = {
env: {
browser: true,
es2021: true,
},
extends: [
'plugin:react/recommended',
'airbnb',
],
parserOptions: {
ecmaFeatures: {
jsx: true,
},
ecmaVersion: 12,
sourceType: 'module',
},
plugins: [
'react',
],
rules: {
'no-console': 1,
'react/prop-types': 0,
'no-underscore-dangle': ['error', { allow: ['__raw'] }],
},
overrides: [
{
files: ['**/*.test.jsx'],
env: { jest: true },
},
],
};
I had the same issue with rxjs/operators as demonstrated in question 67587146. A solution is to add the path to the core modules setting in .eslintrc.js. This solution isn't great, but it's better than disabling the rule.
settings: {
'import/core-modules': ['#apollo/client/link/context']
}
A few others are finding this issue in a recent version of eslint-plugin-import, but there was a fix in v2.23.4, to the package resolution algorithm.
npm update eslint-plugin-import
Read more about the issue

heroku Failed to load config "airbnb" to extend from. Referenced from: /app/.eslintrc.json

I'm trying to deploy an app to Heroku, it's working perfectly in my local. But in Heroku show me: =>
Failed to compile
Failed to load config "airbnb" to extend from.
Referenced from: /app/.eslintrc.json
This error occurred during the build time and cannot be dismissed.
I try to fix it by installing airbn in the Heroku console. But didn't work. I appreciate if you could help me.
Here is my package.json file =>
{
"name": "calculator",
"version": "0.1.0",
"private": true,
"dependencies": {
"#testing-library/jest-dom": "^5.12.0",
"#testing-library/react": "^11.2.6",
"#testing-library/user-event": "^12.8.3",
"big.js": "^6.1.1",
"prop-types": "^15.7.2",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-scripts": "4.0.3",
"web-vitals": "^1.1.2"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"#babel/core": "^7.14.0",
"#babel/eslint-parser": "^7.13.14",
"#babel/plugin-syntax-jsx": "^7.12.13",
"#babel/preset-react": "^7.13.13",
"eslint": "^7.25.0",
"eslint-config-airbnb": "^18.2.1",
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-jsx-a11y": "^6.4.1",
"eslint-plugin-react": "^7.23.2",
"eslint-plugin-react-hooks": "^4.2.0",
"stylelint": "^13.13.1",
"stylelint-config-standard": "^21.0.0",
"stylelint-csstree-validator": "^1.9.0",
"stylelint-scss": "^3.19.0"
}
}
and here my eslint configuration =>
{
"env": {
"browser": true,
"es6": true,
"jest": true
},
"parser": "#babel/eslint-parser",
"parserOptions": {
"ecmaFeatures": {
"jsx": true
},
"ecmaVersion": 2018,
"sourceType": "module"
},
"extends": ["airbnb", "plugin:react/recommended"],
"plugins": ["react"],
"rules": {
"react/jsx-filename-extension": ["warn", { "extensions": [".js", ".jsx"] }],
"react/react-in-jsx-scope": "off",
"import/no-unresolved": "off",
"no-shadow": "off"
},
"ignorePatterns": [
"dist/",
"build/"
]
}
I had to follow these steps and that fixed it.
1| heroku create $APP_NAME --buildpack mars/create-react-app
2| git push heroku HEAD:master
Replace $APP_NAME with the name of your unique app.
here is the documentation link:
github.com/mars/create-react-app-buildpack
I was experiencing the same issues and finally what worked was to move "eslint-config-airbnb" from "devDependencies" to "dependencies" as indicated by eamanola.
Also, my context was a bit different, as I was running a create-react-app and express app.

Resources