9

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.

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.

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:

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!

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....

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.

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?

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.

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...

AymKdn
  • 2,562
  • 19
  • 21
  • Just wondering, do the different `package.json`s show a "recent" time in which they were last edited/updated? Does it only happen for the project you're working on? Or for all projects you have on your machine? – therobinkim Jun 03 '17 at 01:09
  • It doesn't happen on global packages (installed with "-g"), but on the local packages. I don't do a lot of Node development, so it's on the current project... – AymKdn Jun 03 '17 at 07:11

4 Answers4

1

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.

  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.

Levi Mootz
  • 151
  • 4
1

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.

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.

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!

Sean Werkema
  • 3,386
  • 1
  • 27
  • 36
  • Great advice! Thank you. Actually, it looks like 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... – AymKdn Jun 09 '17 at 15:01
  • Well, if it starts up again, you have tools that can explain where it's coming from. You can at least use Process Monitor to prove beyond any doubt that nothing is touching the `package.json` files except `npm` itself, and even knowing that much would be worthwhile information. – Sean Werkema Jun 09 '17 at 15:23
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).

Make sure that you never open it in some editor and save it in anything else than 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:

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.

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.

Community
  • 1
  • 1
rsp
  • 91,898
  • 19
  • 176
  • 156
  • Thanks for the answer. So, no I don't open at all any of the package.json. I'll try the ReadOnly on one of them to see if it makes any difference. No reason to be infected by a malware. And those folders are not shared or synced or mounted – AymKdn Mar 14 '17 at 17:13
0

I am facing a similar problem on files that are on M.2 SSD. The HDD projects run fine, and I have never faced any corruption.

Random files within node_modules get corrupted and serve/build breaks

As of now, I have to keep doing this on my 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.

Deepak Thomas
  • 2,655
  • 4
  • 30
  • 32