I use a corporate computer with Windows 10. I have nodejs v6.10.0 and npm v3.10.10. It's the first time that I install nodejs/npm on this computer.
我使用的是装有Windows 10的公司电脑。我有nodejs v6.10.0和npm v3.10.10。这是我第一次在这台电脑上安装nodejs/npm。
When I install a module (any kind of modules, for example npm install jsdoc
) then everything works fine. I can call my example.js
several times, and all is OK.
当我安装一个模块(任何类型的模块,例如npm安装jsdoc)时,一切都很正常。我可以举个例子。几次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:
如果我检查包的内容。json和SublimeText我得到:
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!
实际上它是所有的包。json文件下node_modules对于这个项目已经损坏....所有的模块!
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....
但是,如果我有一个包裹。json在我的项目文件夹,它不会受到影响,只会.... node_modules文件夹下的
To fix the issue I have to delete node_modules
and reinstall my modules with npm install
. Not really handy. After doing it, my package.json
files are all correct again with the expected content.
为了解决这个问题,我必须删除node_modules并使用npm安装重新安装模块。不方便的。做完之后,我的包。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中的json文件,而不是其他文件夹中的json文件?
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.
我在什么地方读到过,公司代理可以下载一个软件包。json编码错误,但当我安装模块时,包。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 update,或者是我的I/T部门推动了软件更新……
0
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. 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. 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.
在这个阶段,如果是我,我将使用SysInternals Process Monitor:不要特别假设任何东西,只监视并记录系统上的所有I/O,直到有问题的文件开始更改。您可以将Process Monitor设置为记录磁盘操作,然后过滤日志,直到您看到哪个进程在名称中使用.json更改任何内容。可能会有很多日志,您可能需要花一段时间对它们进行筛选,但它至少应该让您有一些东西可以查看,以回答“是什么程序在修改这些文件?”
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; 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. Particularly with SSDs, disks can do some weird things when they start to die.
另一个想法是:如果文件正在更改并且进程监视器没有显示任何内容,那么您的磁盘可能会出现问题。考虑在USB驱动器上完成所有工作一段时间,看看是否会发生同样的结果;如果在驱动器C:而不是驱动器F:(或其他)上的文件被损坏,这可能表明您的磁盘正在开始失败。特别是对于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!
跟踪这些随机文件更改可能很难,但是有一些方法可以确定原因;不要放弃希望,你会找到的。好运!
1
I don't have a specific fix to try, but there are a few general ones that may get you there:
我没有具体的解决办法,但是有一些通用的方法可以帮助你达到目的:
Un-install / Re-install node & npm - Obviously, the first thing you always try if possible.
卸载/重新安装node & npm——显然,如果可能的话,这是您要做的第一件事。
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.
关掉所有不需要的服务/后台应用程序(尤其是扫描仪),看看问题是否会停止。如果是这样,缩小范围,在测试时逐个打开服务。也许你可以编写一个脚本,让你的应用程序运行足够多的时间,让它正常地产生错误。
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有时会让你感到困惑,因为你可以改变权限,有时候看起来好像事情经过了,但是他们没有,你真的没有注意到,直到事情仍然不正常,你再回去检查权限。
0
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(小的和大的endian),但是对包来说是最安全的编码。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.
如果下载后一切正常,那么可能的解决方案是制作每个包。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.
另一件我要研究的事情是如果它发生在每个目录或者可能只有一些你正在共享或同步的目录或者网络安装设备上的目录。