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...
The package. json file is the heart of any Node project. It records important metadata about a project which is required before publishing to NPM, and also defines functional attributes of a project that npm uses to install dependencies, run scripts, and identify the entry point to our package.
npm prune is necessary because the updates to package. json may have removed dependencies. If you do not run npm prune , packages installed by a prior version of package. json will remain in the node_modules directory that would not be there if you freshly downloaded/cloned the project and ran npm install .
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With