【问题标题】:eslint package.json "Failed to parse json" erroreslint package.json“解析json失败”错误
【发布时间】:2018-10-28 09:40:11
【问题描述】:

使用 npm 安装 eslint:

npm install -g eslint

这是我package.json的内容:

$ cat package.json
"extends":"eslint:recommended"

启动我的 eslint:

$ eslint --init
? How would you like to configure ESLint? Use a popular style guide
? Which style guide do you want to follow? Standard
? What format do you want your config file to be in? JSON
Checking peerDependencies of eslint-config-standard@latest
Installing eslint-config-standard@latest, eslint-plugin-import@>=2.8.0, eslint-plugin-node@>=5.2.1, eslint-plugin-promise@>=3.6.0, eslint-plugin-standard@>=3.0.1
npm ERR! file /home/debian9/package.json
npm ERR! code EJSONPARSE
npm ERR! JSON.parse Failed to parse json
npm ERR! JSON.parse Unexpected token : in JSON at position 9 while parsing near '"extends":"eslint:recommended...'
npm ERR! JSON.parse Failed to parse package.json data.
npm ERR! JSON.parse package.json must be actual JSON, not just JavaScript.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/debian9/.npm/_logs/2018-05-18T02_27_01_552Z-debug.log
Successfully created .eslintrc.json file in /home/debian9

使用 eslint 检查test.js 文件:

$ eslint /tmp/test.js
Unexpected token : in JSON at position 9
SyntaxError: Unexpected token : in JSON at position 9
    at JSON.parse (<anonymous>)
    at new IgnoredPaths (/home/debian9/.nvm/versions/node/v9.8.0/lib/node_modules/eslint/lib/ignored-paths.js:176:55)
    at lodash.memoize.optionsObj (/home/debian9/.nvm/versions/node/v9.8.0/lib/node_modules/eslint/lib/util/glob-util.js:115:13)
    at memoized (/home/debian9/.nvm/versions/node/v9.8.0/lib/node_modules/eslint/node_modules/lodash/lodash.js:10551:27)
    at globPatterns.forEach.pattern (/home/debian9/.nvm/versions/node/v9.8.0/lib/node_modules/eslint/lib/util/glob-util.js:162:34)
    at Array.forEach (<anonymous>)
    at Object.listFilesToProcess (/home/debian9/.nvm/versions/node/v9.8.0/lib/node_modules/eslint/lib/util/glob-util.js:158:18)
    at CLIEngine.executeOnFiles (/home/debian9/.nvm/versions/node/v9.8.0/lib/node_modules/eslint/lib/cli-engine.js:518:35)
    at Object.execute (/home/debian9/.nvm/versions/node/v9.8.0/lib/node_modules/eslint/lib/cli.js:189:111)
    at Object.<anonymous> (/home/debian9/.nvm/versions/node/v9.8.0/lib/node_modules/eslint/bin/eslint.js:74:28)

如何修复我的 eslint 配置?

【问题讨论】:

  • 发布您的完整package.json,因为它似乎无效。
  • "扩展":"eslint:recommended"
  • 我的 package.json 中只有一行。

标签: javascript node.js npm eslint


【解决方案1】:

错误很明显:

npm ERR! file /home/debian9/package.json
npm ERR! code EJSONPARSE
npm ERR! JSON.parse Failed to parse json

package.json 必须包含有效 JSON。

所以运行:npm init 生成一个有效的,然后添加你的 eslintConfig 到它

{
  "name": "test",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "eslintConfig": {
    "extends": "eslint:recommended"
  }
}

你所有的 eslint 配置,必须在 package.jsoneslintConfig 属性内。你可以阅读here中的文档

【讨论】:

    猜你喜欢
    • 2016-06-27
    • 2017-10-11
    • 1970-01-01
    • 1970-01-01
    • 2018-07-14
    • 2019-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多