简体   繁体   English

一段时间后 package.json 文件已损坏

[英]The package.json files are corrupted after a while

I use a corporate computer with Windows 10. I have nodejs v6.10.0 and npm v3.10.10.我使用装有 Windows 10 的公司计算机。我有 nodejs v6.10.0 和 npm v3.10.10。 It's the first time that I install nodejs/npm on this computer.这是我第一次在这台电脑上安装nodejs/npm。

When I install a module (any kind of modules, for example npm install jsdoc ) then everything works fine.当我安装一个模块(任何类型的模块,例如npm install jsdoc )时,一切正常。 I can call my example.js several times, and all is OK.我可以多次调用我的example.js ,一切正常。

But after a while (random period) I cannot run my program anymore because I get the below error:但是过了一段时间(随机时间段),我无法再运行我的程序,因为出现以下错误:

>node example.js
module.js:96
    throw e;
    ^

SyntaxError: Error parsing C:\my_path\node_modules\some_module\package.json: Unexpected token x in JSON at position 0

If I check the content of the package.json with SublimeText I got:如果我用 SublimeText 检查package.json的内容,我得到:

78c0 b658 72a3 e0f5 7832 e7d4 b5ee dcc8
8f00 9951 3b8a cbd5 db7f 4556 5e8b e88d
087d 9bb8 ff15 9acb 0a09 7aaf afd3 ced2
3aa9 e2c5 7e7b c4a1 7b82 a332 2848 83ed
adca d7e8 3228 5537 64eb 3105 2338 6ae2
[...]

And actually it's all the package.json files under node_modules for this project that have been corrupted.... For all modules!实际上是这个项目的node_modules下的所有package.json文件都被损坏了......对于所有模块!

However, if I have a package.json in my project folder, it won't be impacted, only the ones under node_modules folder will be....但是,如果我的项目文件夹中有package.json ,它不会受到影响,只有node_modules文件夹下的那些会......

To fix the issue I have to delete node_modules and reinstall my modules with npm install .为了解决这个问题,我必须删除node_modules并使用npm install重新安装我的模块。 Not really handy.不是很方便。 After doing it, my package.json files are all correct again with the expected content.完成后,我的package.json文件再次全部正确,内容符合预期。

I thought it could be related to our McAfee anti-virus, but why it will only impact the package.json files under node_modules , and not the ones that are in other folders?我认为它可能与我们的 McAfee 防病毒有关,但为什么它只会影响node_modules下的package.json文件, node_modules影响其他文件夹中的文件?

I read somewhere that a corporate proxy could download a package.json with the wrong encoding, but when I install my modules, the package.json are totally normal.我在某处读到公司代理可以下载编码错误的package.json ,但是当我安装我的模块时, package.json完全正常。

So if anyone has any idea/lead, I'll appreciate!因此,如果有人有任何想法/线索,我将不胜感激!

EDIT : The corruption stopped to happen since the last release of npm (5.x) ... I don't know if it's related to it, or maybe a Windows update installed, or my I/T dept pushed a software update...编辑:自上次发布 npm (5.x) 以来,损坏停止发生......我不知道它是否与它有关,或者可能安装了 Windows 更新,或者我的 I/T 部门推送了软件更新。 ..

I don't have a specific fix to try, but there are a few general ones that may get you there:我没有要尝试的特定修复方法,但有一些通用的修复方法可以帮助您:

  1. Un-install / Re-install node & npm - Obviously, the first thing you always try if possible.卸载/重新安装 node 和 npm - 显然,如果可能的话,你总是尝试的第一件事。

  2. Turn off all unneeded services / background apps (especially scanners) and see if the problem stops.关闭所有不需要的服务/后台应用程序(尤其是扫描仪)并查看问题是否停止。 If so, narrow your scope and turn services on one-by-one while testing.如果是这样,请在测试时缩小范围并逐一启用服务。 Maybe make a script to run your app enough times that it would normally generate the error.也许制作一个脚本来运行你的应用程序足够多的时间,它通常会产生错误。

  3. Is there an on-save build tool running?是否有正在运行的保存构建工具? Gulp / Grunt?吞咽/咕噜声? This could also be the culprit.这也可能是罪魁祸首。

I'm guessing if you try the first two suggestions you'll find your problem.我猜如果你尝试前两个建议,你会发现你的问题。

Also, the read-only thing you mention is odd, but are you sure the proper policies are in place that would allow you to set those permissions?此外,您提到的只读内容很奇怪,但是您确定有适当的策略可以让您设置这些权限吗? Windows is confusing sometimes as you can change permissions and sometimes it really looks like things went through ok, but they didn't and you really don't notice until things still aren't working and you go back to check the permissions again. Windows 有时会令人困惑,因为您可以更改权限,有时看起来事情确实通过了,但实际上并没有,而且您真的没有注意到,直到事情仍然无法正常工作并且您再次返回检查权限。

At this stage, if it was me, I'd be using the SysInternals Process Monitor : Don't assume anything specifically, and just monitor and log all I/O on your system until the files in question start changing.在这个阶段,如果是我,我会使用 SysInternals Process Monitor :不要特别假设任何事情,只需监视和记录系统上的所有 I/O,直到有问题的文件开始更改。 You can set Process Monitor up to record disk actions, and then filter the logs until you see which process is actually changing anything with .json in the name.您可以将 Process Monitor 设置为记录磁盘操作,然后过滤日志,直到您看到哪个进程实际上正在更改名称中.json任何内容。 There will likely be a lot of logs, and you might have to spend a while sifting through them, but it should at least give you something to look at to at least answer the question "What program is changing these files?"可能会有很多日志,你可能需要花一些时间来筛选它们,但它至少应该给你一些东西来至少回答“什么程序正在改变这些文件?”的问题。 instead of having to guess.而不必猜测。

One other thought: If the files are changing and Process Monitor doesn't show anything at all, you may have a disk that is going bad.另一个想法:如果文件正在更改并且进程监视器根本没有显示任何内容,则您的磁盘可能会变坏。 Consider doing all your work on a USB drive for a little while and see if the same results happen;考虑在 USB 驱动器上完成所有工作一段时间,看看是否会出现相同的结果; if the files are getting corrupted on drive C: but not on drive F: (or whatever), that may suggest your disk is starting to fail.如果文件在驱动器C:上损坏,但在驱动器F:或其他)上没有损坏,则可能表明您的磁盘开始出现故障。 Particularly with SSDs, disks can do some weird things when they start to die.特别是对于 SSD,磁盘在开始消亡时会做一些奇怪的事情。

Tracking these kinds of random file changes can be hard, but there are ways that you can identify the cause;跟踪这些类型的随机文件更改可能很困难,但您可以通过多种方法确定原因; don't give up hope, and you'll find it.不要放弃希望,你会找到它的。 Good luck!祝你好运!

This is very strange.这很奇怪。 The only reason not related to malware than can be causing that would be a problem with encoding.与恶意软件无关的唯一原因可能是编码问题。 The only valid encodings for JSON are UTF-8, UTF-16 and UTF-32 (both little and big endian) but the safest encoding for package.json is definitely UTF-8 (without BOM). JSON 唯一有效的编码是 UTF-8、UTF-16 和 UTF-32(小端和大端),但 package.json 最安全的编码绝对是 UTF-8(没有 BOM)。

Make sure that you never open it in some editor and save it in anything else than UTF-8.确保您永远不要在某些编辑器中打开它并将其保存为 UTF-8 以外的任何其他格式。

The only other reason for that corruption could be indeed some malware.这种损坏的唯一其他原因可能确实是一些恶意软件。

See those answers for more info on JSON and character encoding:有关 JSON 和字符编码的更多信息,请参阅这些答案:

If everything is fine after the download, then a possible workaround would be to make every package.json file read-only after the successful installation and see when you will see an error that something cannot write them.如果下载后一切正常,那么可能的解决方法是在成功安装后将每个package.json文件设为只读,然后查看何时会看到某些内容无法写入它们的错误。

Another thing I would take a look at is if it happens in every directory or maybe only some directories that you are sharing or syncing or that are on network mounted devices.我要考虑的另一件事是它是否发生在每个目录中,或者可能仅发生在您共享或同步的某些目录中,或者发生在网络安装的设备上。

I am facing a similar problem on files that are on M.2 SSD.我在 M.2 SSD 上的文件上面临类似的问题。 The HDD projects run fine, and I have never faced any corruption. HDD 项目运行良好,我从未遇到任何损坏。

Random files within node_modules get corrupted and serve/build breaks node_modules随机文件被损坏并且服务/构建中断

As of now, I have to keep doing this on my SSD:到目前为止,我必须在我的 SSD 上继续这样做:

rm -rf node_modules
npm install

It's a very tedious process, but I have no other option.这是一个非常乏味的过程,但我别无选择。 I will update the answer if I find some solution.如果我找到一些解决方案,我会更新答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM