0

I get the following error while installing git-stats on ubuntu 18.

╰─ sudo npm i -g git-stats
npm WARN deprecated debug@4.2.0: Debug versions >=3.2.0 <3.2.7 || >=4 <4.3.1 have a low-severity ReDos regression when used in a Node.js environment. It is recommended you upgrade to 3.2.7 or 4.3.1. (https://github.com/visionmedia/debug/issues/797)
/usr/bin/git-stats -> /usr/lib/node_modules/git-stats/bin/git-stats

> git-stats@2.10.13 postinstall /usr/lib/node_modules/git-stats
> node scripts/migration/2.0.0.js

internal/modules/cjs/loader.js:834
  throw err;
  ^

Error: Cannot find module '/usr/lib/node_modules/git-stats/scripts/migration/2.0.0.js'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:831:15)
    at Function.Module._load (internal/modules/cjs/loader.js:687:27)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)
    at internal/main/run_main_module.js:17:47 {
  code: 'MODULE_NOT_FOUND',
  requireStack: []
}
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@~2.1.2 (node_modules/git-stats/node_modules/chokidar/node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@2.1.3: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! git-stats@2.10.13 postinstall: `node scripts/migration/2.0.0.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the git-stats@2.10.13 postinstall script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/aman/.npm/_logs/2020-12-10T03_19_40_806Z-debug.log

As far as I can tell the file migration/2.0.0.js is present in the installation folder created under /usr/lib/node_modules (found this out by doing ls -al while the installation was running). Not sure why NPM is not working. I am running Ubuntu 18.

Daniel_Knights
  • 7,940
  • 4
  • 21
  • 49
Aman Deep Gautam
  • 8,091
  • 21
  • 74
  • 130

2 Answers2

4

I cannot reproduce the issue on my end, but I would investigate this issue like this:

The cause of the error

Error: Cannot find module '/usr/lib/node_modules/git-stats/scripts/migration/2.0.0.js'

This error appears when the script is simply missing from the disk. If you run ls /usr/lib/node_modules/git-stats/scripts/migration/2.0.0.js you will get a similar error saying file is not found.

Is the migration folder included in the package?

According to the logs you are installing 2.10.13 which does have the scripts/ folder whitelisted in the files from package.json. Hence, scripts/migration/2.0.0.js should be available.

Debugging the error

I would suggest checking what you have in the /usr/lib/node_modules/git-stats/ folder. Make sure there is a scripts folder and in it the migration folder containing 2.0.0.js:

ls /usr/lib/node_modules/git-stats/
ls /usr/lib/node_modules/git-stats/scripts
ls /usr/lib/node_modules/git-stats/scripts/migration
ls /usr/lib/node_modules/git-stats/scripts/migration/2.0.0.js

Doing it the right way

I would suggest not installing npm packages via sudo. In my experience I found nvm a better way to manage the versions of Node.js on MacOS/Linux/Windows.

At this point you can uninstall Node.js if you installed it from the Ubuntu packages -- note this will remove all the global packages you have installed:

sudo apt-get purge --auto-remove nodejs npm
sudo rm -rf /usr/lib/node_modules
rm -rf ~/.npm

Then, install nvm (Node Version Manager):

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.2/install.sh | bash

Open a new terminal instance (to reload the .bashrc file) and install one of the latest LTS (long term supported) versions of Node:

nvm ls-remote

As of today v14.15.4 seems to be the latest LTS, so you can do:

nvm install v14.15.4

Finally, install git-stats with the new version of Node.js/npm and it should work fine, without sudo:

npm i -g git-stats
Ionică Bizău
  • 109,027
  • 88
  • 289
  • 474
0

The first thing to try would be a fresh reinstall of node_modules, and in the process cleaning the npm cache:

sudo rm -rf node_modules
sudo npm cache clean -f
sudo npm i && sudo npm i -g git-stats

If it's still unsuccessful, you may want to consider explicitly setting your NODE_PATH so the module reference targets the correct installation directory. For more directions on how to determine the correct NODE_PATH, check out this answer:

https://stackoverflow.com/a/14515868/10976382

Len Joseph
  • 1,406
  • 10
  • 21