简体   繁体   中英

"No files matching the pattern "src" were found." when using "npx eslint src" or "npx eslint src/"

I've just set up a create-react-app project with typescript. I've added eslint with npx eslint --init . When I run npx eslint src/ or npx eslint src I get an error:

Oops! Something went wrong! :(

ESLint: 8.18.0

No files matching the pattern "src" were found.
Please check for typing mistakes in the pattern.

However, if I use this command: npx eslint src/* then it works. This is fine, but some guides and stackoverflow comments ( eg on here ) I've seen show that the commands that don't work for me are working for seemingly everyone else. What am I doing wrong?

eslint.js

module.exports = {
  env: {
    browser: true,
    es2021: true,
    jest: true,
  },
  extends: ["plugin:react/recommended", "standard"],
  parser: "@typescript-eslint/parser",
  parserOptions: {
    ecmaFeatures: {
      jsx: true,
    },
    ecmaVersion: "latest",
    sourceType: "module",
  },
  plugins: ["react", "@typescript-eslint"],
  rules: {
    semi: "off",
    quotes: "off",
    "space-before-function-paren": "off",
  },
  ignorePatterns: ["**/*.css", "**/*.svg"],
};

package.json :

 "dependencies": {
    "@testing-library/jest-dom": "^5.14.1",
    "@testing-library/react": "^13.0.0",
    "@testing-library/user-event": "^13.2.1",
    "@types/jest": "^27.0.1",
    "@types/node": "^16.7.13",
    "@types/react": "^18.0.0",
    "@types/react-dom": "^18.0.0",
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "react-scripts": "5.0.1",
    "typescript": "^4.4.2",
    "web-vitals": "^2.1.0"
  },
    "devDependencies": {
    "@typescript-eslint/eslint-plugin": "^5.30.0",
    "@typescript-eslint/parser": "^5.30.0",
    "eslint": "^8.0.1",
    "eslint-config-standard": "^17.0.0",
    "eslint-plugin-import": "^2.25.2",
    "eslint-plugin-n": "^15.0.0",
    "eslint-plugin-promise": "^6.0.0",
    "eslint-plugin-react": "^7.30.1"
  },

Okay the comments I left were incorrect. This question left me reading about ESLint, how it works, and how the npm / npx commands differ.

So here is the deal

NOTE: "I use the syntax $(pwd) to refer to the current-working-directory, because it's not a pseudo representation of a path, but rather a command, wrapped as such, that it can be interpreted, and ran, by the BASH shell."

In the comments, I wrote about eslint resolving paths, and its inability to infer the meaning of the CLI passed argument src , as shown in this snippet:

  /* COMMAND-1 */

  $ eslint src

Thinking back on it, that was a really dumb comment to make, that I shouldn't have said. The reason it wasn't smart, is because, the way the path is resolved completely changes when you use the following:

  /* COMMAND-2 */

  $ npx eslint src

Looking at the two commands together, side by side, is helpful in the sense that it allows us to draw a side by side comparison, however, there really isn't much to compare, because of how different these two commands are.

So the first command, COMMAND-1 , resolves src as if it were a path, however, COMMAND-2 executes an ESLint internal command, hence the "X" in npx , which is the same as executing...

npm exec or npm x

In other words, the following; npx eslint src can also be executed using npm x eslint src , or npm exec eslint src .

So, as stated above, **the argument src is not a system file-path , but rather, it's an internal ESLint command that offers a cute little CLI printout of your errors. ITS ACTUALLY PRETTY COOL


Initially the Command didn't work for me either

The problem I had, looks, like it was the same problem your having.

The solution is quite simple.
  1. Update NPM to the latest version.
  2. Update NPX to the latest version. (more on this below)
  3. Update Node.js to the latest version
  4. Update ESLint to the latest version
  5. Update all of your proj's pkgs, then audit them with the fix command (more on this below).

So NPM comes with the NPX command embedded in it, and technically, npx is the npm command, just ran as npm x or npm exec , however; from the standpoint of your file-system, (in other words, looking at the commands from the context of /usr/bin/... ) they are two different commands.

So I know alot about ubuntu because it was my OS for 7 years. About a year ago I switched to Fedora 36: Workstation and was glad I did. I made the change because of the issues associated with installing Node.js & NPM. Ubuntu makes it difficult. The reason I use Linux in the first place is because software runs in an environment that suited for running software, and not suited for protecting the proprietary licenses associated with the software. Anyways, long story made short... the best way for you to upgrade NPX (which means upgrading NPM, which means you need to upgrade Node.js), is to install the latest version of Node.js, go into the directory, and create symbolic links for npm , node , npx , in your /usr/bin directory and then upgrade eslint (globally) to version v8.18.0 . You may also need to create a symbolic link for ESLint.




On a final note, just to point out somthing. You notice your ESLint version your package.json file is v8.18.0 ? and the version in the error you are getting is v8.1.0 ?

That is because your local & global eslint installs are different versions of eslint. You should try to always keep everything the same version.

Right now your system is trying to execute npx eslint src as if src is a path, but one everything is updated, you should have the software needed, for eslint & npm to know that its an ESLint command your trying to execute, not a path.

我认为你应该在 tsconfig.ts 文件中有这个属性:

"baseUrl": "."

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM