eslint package.json“无法解析json”错误

时间:2018-05-18 02:30:11

标签: javascript node.js npm eslint

使用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配置?

1 个答案:

答案 0 :(得分: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配置必须位于eslintConfig的{​​{1}}属性中。您可以阅读here

中的文档