'Error while loading rule 'prettier/prettier': context.getPhysicalFilename is not a function

I am learning react js. I am a very beginner at this topic. But when I am doing setup to create a react environment I got an error. I have tried to solve the problem by Charles Stover blog in medium. But I got an error Command "up" not found.

Here's my index.js file:

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import reportWebVitals from './reportWebVitals';
ReactDOM.render(
   <React.StrictMode>
      <App />
   </React.StrictMode>,
document.getElementById('root')
);
reportWebVitals();

My package.json file:

{
  "name": "myreact",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^5.11.4",
    "@testing-library/react": "^11.1.0",
    "@testing-library/user-event": "^12.1.10",
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "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",
    "lint": "yarn add -D prettier && yarn add -D babel-eslint && npx install-peerdeps --dev eslint-config-airbnb && yarn add -D eslint-config-prettier eslint-plugin-prettier"
  },
  "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-eslint": "^10.1.0",
    "eslint": "7.2.0",
    "eslint-config-airbnb": "18.2.1",
    "eslint-config-prettier": "^8.3.0",
    "eslint-plugin-import": "2.22.1",
    "eslint-plugin-jsx-a11y": "6.4.1",
    "eslint-plugin-prettier": "^4.0.0",
    "eslint-plugin-react": "7.21.5",
    "eslint-plugin-react-hooks": "1.7.0",
    "prettier": "^2.3.2"
  }
}

When I am running yarn start in terminal, I am getting an error.

Error while loading rule 'prettier/prettier': context.getPhysicalFilename is not a function
Occurred while linting F:\react-projects\myreact\src\index.js

Now what should I do?



Solution 1:[1]

I got two different solution.

  1. remove
"prettier/prettier": [
            "error",
            {
                "trailingComma": "es5",
                "singleQuote": true,
                "printWidth": 100,
                "tabWidth": 4,
                "semi": true,
                "endOfLine": "auto"
            }
        ]

from .eslintrc folder and change react script version from 4.0.3 to 4.0.1 and run npm install.

  1. Use yarn upgrade -R eslint and change react script version from 4.0.3 to 4.0.1 and run npm install.

Solution 2:[2]

Upgrade eslint

I had the same error with Next.js:

package.json

{
  "name": "with-eslint",
  "version": "1.0.0",
  "scripts": {
    "dev": "next",
    "build": "next build",
    "start": "next start",
    "lint": "next lint",
    "type-check": "tsc"
  },
  "license": "MIT",
  "dependencies": {
    "install": "0.13.0",
    "next": "12.0.7",
    "react": "17.0.2",
    "react-dom": "17.0.2"
  },
  "devDependencies": {
    "@types/node": "12.12.21",
    "@types/react": "17.0.2",
    "@types/react-dom": "17.0.1",
    "eslint": "7.24.0",
    "eslint-config-next": "12.0.7",
    "eslint-config-prettier": "7.2.0",
    "eslint-plugin-prettier": "4.0.0",
    "prettier": "2.5.1",
    "typescript": "4.5.4"
  },
  "prettier": {
    "printWidth": 80,
    "semi": false,
    "singleQuote": true
  }
}

.eslintrc.json

{
  "extends": ["eslint:recommended", "next", "prettier"],
  "plugins": ["prettier"],
  "rules": {
    "prettier/prettier": "error"
  }
}

pages/index.tsx

import React from 'react'

const Home = () => {
  let n: number
  let s = 'abc'
  s = "abc"
  let unused
  if (false) {
    React.useEffect(() => 1)
  }
  return (
    <div>
      <script src="https://fake-script.com" />
      <p>Home</p>
    </div>
  )
}

export default Home

and moving to:

    "eslint": "8.5.0",

solved it.

Solution 3:[3]

I just updated react-scripts to 4.0.1 and eslint to 7.32.0. It worked.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Ciro Santilli Путлер Капут 六四事
Solution 3 Wallace Eduardo