node.js npm“无法解析json”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31454607/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 18:58:36  来源:igfitidea点击:

npm "failed to parse json"

node.jsubuntunpm

提问by kovogel

When I'm trying to install express with npm I always get the following error:

当我尝试使用 npm 安装 express 时,我总是收到以下错误:

Failed to parse json
No data, empty input at 1:1
File: /root/.npm/inherits/2.0.1/package/package.json
Failed to parse package.json data.
package.json must be actual JSON, not just JavaScript.

This is not a bug in npm.
Tell the package author to fix their package.json file. JSON.parse

What am I doing wrong?

我究竟做错了什么?

 sudo npm install -g express

OS is Ubuntu 12.04 (precise) armhf

操作系统是 Ubuntu 12.04(精确)armhf

回答by kovogel

Thanks to Jivingsfrom this comment:

感谢来自此评论的Jivings

npm cache clean

solved the problem.

解决了这个问题。

回答by adeguk Loggcity

I had the same problem but "npm cache clean" didnt resolve it for me. I had to go back to my package.json and realize i had comma where it shouldn't i suppose as shown below:

我遇到了同样的问题,但“npm cache clean”并没有为我解决。我不得不回到我的 package.json 并意识到我在不应该的地方有逗号,如下所示:

},
  "devDependencies": {
    "axios": "^0.15.3",
    "bootstrap-sass": "^3.3.7",
    "cross-env": "^3.2.4",
    "jquery": "^3.1.1",
    "laravel-mix": "0.*",
    "lodash": "^4.17.4",
    "vue": "^2.1.10",
  }

after the "vue..." so I deleted that and every went back to normal. So it is worth checking the package.json file first before runningnpm cache clean

在“vue ...”之后,我删除了它,然后一切都恢复正常。所以值得在运行前先检查 package.json 文件npm cache clean

回答by Ashish Mishra

Mostly, this error is due to a syntax error in package.json file. In my case, the opening curly brace for dependencies object in package.json was missing:-

大多数情况下,此错误是由于 package.json 文件中的语法错误造成的。就我而言,package.json 中的依赖项对象的左花括号丢失了:-

Code--------------------------------

代码 - - - - - - - - - - - - - - - -

{
  "name": "psrxjs",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": 
    "rxjs": "^5.4.3"
  }
}


回答by B Ii

In Laravel project:

在 Laravel 项目中:

  1. Delete 'node_modules' folder;
  2. npm cache clean
  3. npm update
  1. 删除“node_modules”文件夹;
  2. npm 缓存清理
  3. 更新

回答by Avijit Majhi

I also got the same error message while run npm install, first run npm package.jsonto check errors in package.json file, if not then run npm cache clean

我在运行时也收到相同的错误消息npm install,首先运行npm package.json以检查 package.json 文件中的错误,如果没有则运行npm cache clean

回答by Harry_C

I faced this problem several times before I got used to using NPM. Most off the time it was because I failed to use npm init before npm install

在我习惯使用 NPM 之前,我多次遇到过这个问题。大多数时候是因为我没有在 npm install 之前使用 npm init

回答by Damon Wu

Please check with unused white spaces inside package.json file, it might cause by extra white spaces.

请检查 package.json 文件中未使用的空格,这可能是由多余的空格引起的。

回答by MD SHAYON

I think you may be did some change in package.jsonand that is not valid

我认为您可能对package.json进行了一些更改,但这是无效的

  1. Delete node_modules
  2. Delete package.json
  3. Create a new NPM package with
  1. 删除node_modules
  2. 删除package.json
  3. 创建一个新的 NPM 包

npm init

npm init

  1. install all of your package once again
  1. 再次安装所有软件包

npm install express

npm install express

回答by Prashant Yalatwar

I solved the issue using below steps:

我使用以下步骤解决了这个问题:

  1. Delete node_modulesfolder

  2. Delete package-lock.jsonfile

  3. Run npm install

  4. Run npm start

  1. 删除node_modules文件夹

  2. 删除package-lock.json文件

  3. npm install

  4. npm start

回答by Jexon

For those of you who are new to this like me, I forgot to initialize my JSON package with the npm init command.

对于像我这样的新手,我忘记使用 npm init 命令初始化我的 JSON 包。