npm install 无法读取 package.json [英] npm install cannot read package.json
问题描述
我正在尝试管理我的节点包依赖项.我希望能够通过运行命令来安装所有必需的依赖项,从我读过的内容来看,实现此目的的一种方法是使用 package.json
文件并运行 npm安装
.所以我的 JSON 文件看起来像这样:
I am trying to manage my node package dependencies. I'd like to be able to install all the required dependencies by running a command, and from what I have read, one way to achieve this is using a package.json
file and running npm install
. So my JSON file looks like this:
{
"name": "Name-Of-The-Thing",
"description": "The Thing's Name",
"author": "The Dude <the.dude@dudethinking.com>",
"dependencies": {
"mocha":">= 1.12.0",
"mocha-phantomjs":">= 3.1.0",
"chai":">= 1.7.2",
"phantomjs":">= 1.9.1"
}
}
但是npm install
报如下错误:
npm ERR! Failed to parse json
npm ERR! Unexpected token ?
npm ERR! File: C:\Path\To\The\Thing\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 Windows_NT 6.2.9200
npm ERR! command "C:\\Program Files\\nodejs\\\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "test"
npm ERR! cwd C:\Path\To\The\Thing
npm ERR! node -v v0.8.15
npm ERR! npm -v 1.1.66
npm ERR! file C:\Path\To\The\Thing\package.json
npm ERR! code EJSONPARSE
npm ERR!
npm ERR! Additional logging details can be found in:
npm ERR! C:\Path\To\The\Thing\npm-debug.log
npm ERR! not ok code 0
有人知道为什么吗?
推荐答案
正确答案:
您的编辑器向 JSON 文件添加了一个字节顺序标记,这使得八位字节流成为无效的 JSON 文本.
Your editor adds a byte-order-mark to the JSON file, which makes the octet-stream an invalid JSON text.
JSON RFC 说:
JSON 文本应以 Unicode 编码.默认编码是UTF-8.
JSON text SHALL be encoded in Unicode. The default encoding is UTF-8.
因为 JSON 文本的前两个字符将始终是 ASCII字符 [RFC0020],可以确定一个八位字节通过查看流是 UTF-8、UTF-16(BE 或 LE)或 UTF-32(BE 或 LE)在前四个八位字节中的空值模式处.
Since the first two characters of a JSON text will always be ASCII characters [RFC0020], it is possible to determine whether an octet stream is UTF-8, UTF-16 (BE or LE), or UTF-32 (BE or LE) by looking at the pattern of nulls in the first four octets.
00 00 00 xx UTF-32BE
00 xx 00 xx UTF-16BE
xx 00 00 00 UTF-32LE
xx 00 xx 00 UTF-16LE
xx xx xx xx UTF-8
您提到的错误报告已因此关闭.
根据我的理解,任何有效的 ASCII 编码文本也恰好是有效的 UTF-8,因此加上缺少 BOM,它解释了为什么它现在可以按预期工作.
From my understanding, any valid ASCII encoded text also happens to be valid UTF-8, so together with the absence of the BOM it explains why it now works as expected.
一般来说,我认为您应该将文本编辑器设置为以 UTF-8 格式保存文件,而没有字节顺序标记.请参阅 UTF-8 和 UTF-8 之间有什么不同,没有BOM? 供讨论.根据 Node.js 源代码需要什么编码? ,Node.js 将接受以这种方式编码的 JS 源文件中的非 ASCII 字符.当您想在源代码中的某处嵌入非 ASCII 字符串时,这会很方便.
In general, I think you should set up your text editor to save files in UTF-8, without a byte-order-mark. See What's different between UTF-8 and UTF-8 without BOM? for discussion. Per What encoding is expected for Node.js source code? , Node.js would accept non-ASCII characters in JS source files encoded this way. This can be handy when you want to embed a non-ASCII string somewhere in the source code.
这篇关于npm install 无法读取 package.json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!