node.js npm 安装错误 - 无效的 package.json

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18743670/
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 15:36:01  来源:igfitidea点击:

npm install error - invalid package.json

jsonnode.jsexpressoauth-2.0npm

提问by tldr

I'm using thistutorial to understand implementing oauth2 in Node.js.

我正在使用教程来了解在 Node.js 中实现 oauth2。

This is my package.json:

这是我的 package.json:

{
  "name": "application-name",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "start": "coffee -w server.coffee"
  },  
  "dependencies": {
    "express": "3.3.4",
    "jade": "*",
    "coffee-script": "latest",
    "passport-http": "latest",
    "passport-http-bearer": "latest",
    "passport-oauth2-client-password": "latest",
    "oauth2orize": "latest",
    "debug" "~0.7.2"
  }
}

I'm getting the error:

我收到错误:

npm ERR! install Couldn't read dependencies
npm ERR! Failed to parse json
npm ERR! Unexpected string
npm ERR! File: /Users/jashua/Desktop/auth/package.json
npm ERR! Failed to parse package.json data.
npm ERR! package.json must be actual JSON, not just JavaScript.
npm ERR! 
npm ERR! This is not a bug in npm.
npm ERR! Tell the package author to fix their package.json file. JSON.parse

npm ERR! System Darwin 12.3.0
npm ERR! command "node" "/usr/local/bin/npm" "install"
npm ERR! cwd /Users/jashua/Desktop/auth
npm ERR! node -v v0.10.15
npm ERR! npm -v 1.3.5
npm ERR! file /Users/jashua/Desktop/auth/package.json
npm ERR! code EJSONPARSE
npm ERR! 
npm ERR! Additional logging details can be found in:
npm ERR!     /Users/me/Desktop/auth/npm-debug.log
npm ERR! not ok code 0

Any ideas what's wrong with my package.json?

知道我的 package.json 有什么问题吗?

回答by hexacyanide

The error means exactly what it states, the JSON in your package.jsonis not valid and can't be parsed properly. Change "debug" "~0.7.2"to "debug": "~0.7.2".

该错误的含义正是它所说的,您中的 JSONpackage.json无效且无法正确解析。更改"debug" "~0.7.2""debug": "~0.7.2"

回答by mightypile

I arrived at this page due to the same npm error, but a different cause. After viewing the log files, I realized that npm did not like my comment on the top line of the file.

由于相同的 npm 错误,我到达此页面,但原因不同。查看日志文件后,我意识到 npm 不喜欢我在文件第一行的评论。

// package.json
{ ...
}

After removing all comments, npm ran through everything beautifully. My comment was not necessary, so I'm fine as it is. But if you require comments within your xml, see How do I add comments to package.json for npm install?

删除所有评论后,npm 完美地运行了所有内容。我的评论没有必要,所以我很好。但是,如果您需要在 xml 中添加注释,请参阅如何向 package.json 添加注释以进行 npm 安装?

回答by Arthur

I had the same issue caused by changes that I had made to my package.jsonfile. I just reverted to my backup version of the package.jsonfile. I attached that below if anyone else needs one:

由于对package.json文件所做的更改,我遇到了同样的问题。我刚刚恢复到package.json文件的备份版本。如果其他人需要,我在下面附上了:

{"name": "OpenShift-Sample-App",
 "version": "1.0.0",
 "description": "OpenShift Sample Application",
 "keywords": [
   "OpenShift",
   "Node.js",
   "application",
   "openshift"
 ],
 "author": {
   "name": "OpenShift",
   "email": "[email protected]",
   "url": "http://www.openshift.com/"
 },
 "homepage": "http://www.openshift.com/",
 "repository": {
   "type": "git",
   "url": "https://github.com/openshift/origin-server"
 },
 "engines": {
   "node": ">= 0.6.0",
   "npm": ">= 1.0.0"
 },
 "dependencies": {
   "express": "~3.4.4"
 },
 "devDependencies": {},
 "bundleDependencies": [],
 "private": true,
 "main": "server.js"
}

回答by mik01aj

Sometimes you might have some weird unicode chars in your package.json. For example you might have \u00A0(unicode non-breaking space) instead of a space somewhere. It looks the same in editor, so beware.

有时,您的package.json. 例如,您可能有\u00A0(Unicode 不间断空格)而不是某处的空格。它在编辑器中看起来是一样的,所以要小心。

(I just had such problem after copying a small piece of config from a blog.)

(我刚刚从博客中复制了一小段配置后遇到了这样的问题。)

回答by Himabindu Appali

Use this command to fix package.json related issue.

使用此命令修复 package.json 相关问题。

npm cache clean

npm 缓存清理

回答by Anti-puff

My project was in D:\wwwroot\Projetos 2017\Android\PluginTest\PluginTest\plugins\cordova-plugin-iservice

我的项目在 D:\wwwroot\Projetos 2017\Android\PluginTest\PluginTest\plugins\cordova-plugin-iservice

The problem is space between Projetos(here)2017

问题是 Projetos(here)2017 之间的空间

I removed space to: D:\wwwroot\Projetos2017\Android\PluginTest\PluginTest\plugins\cordova-plugin-iservice

我删除了空间:D:\wwwroot\Projetos2017\Android\PluginTest\PluginTest\plugins\cordova-plugin-iservice

and Work fine

并且工作正常

回答by Robert Stefanz

C:\Program Files\nodejs>npm cache verify

C:\Program Files\nodejs>npm cache verify

Fixes it on Windows 10, run it with an Administrator command prompt Window.

在 Windows 10 上修复它,使用管理员命令提示符窗口运行它。