0

I am working on Debian Stable Linux which is updated and working very well.

I have installed a compute-pcorr package globally with following npm command:

$ sudo npm install compute-pcorr -g

It is listed with following npm command:

$  npm list -g --depth=0 
/usr/local/lib
├── compute-pcorr@1.0.0
└── ml-stat@1.3.3

I am now trying following code demo code from its homepage:

var pcorr = require( 'compute-pcorr@1.0.0' ); 

var x = [ 1, 2, 3, 4, 5 ]; 
var y = [ 5, 4, 3, 2, 1 ];

var mat = pcorr( x, y );
Console.log("Correlation Matrix: "+ mat)

However, when I run above code, I get error:

$ node compute_pcorr_usage.js 

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

Error: Cannot find module 'compute-pcorr@1.0.0'

Following in code is also not working:

var pcorr = require( 'compute-pcorr' ); 

I also tried to reinstall package with following command, but error persists:

sudo npm install compute-pcorr -g --save

Where is the problem and how can it be solved?

rnso
  • 23,686
  • 25
  • 112
  • 234
  • You also have to add it locally by ```npm install compute-pcorr```. I tried and it worked. – Hazik Arshad May 17 '23 at 06:08
  • So, I should not install globally with -g option? – rnso May 17 '23 at 06:09
  • 1
    Does this answer your question? [NodeJS require a global module/package](https://stackoverflow.com/questions/15636367/nodejs-require-a-global-module-package) – Justinas May 17 '23 at 06:12
  • Yes. There's no need. – Hazik Arshad May 17 '23 at 06:13
  • Global packages are usually for things like command line tools, which after you install you get a command which can be used everywhere, like `tsc`, `pkg`, etc. For dependencies which your project depends on, i.e. the code need to be ship with your project, you install it locally. – Ricky Mo May 17 '23 at 06:13
  • Have you created `package.json` using `npm init` before `npm install compute-pcorr`? – Jordy May 17 '23 at 06:20
  • No, but my code is now working! package.json has been automatically created and populated. – rnso May 17 '23 at 06:24
  • What is the role of `--save` option? When is that needed? – rnso May 17 '23 at 12:51

1 Answers1

1

This issue happens when the library is not installed locally.
Try installing locally in your directory with the command npm install compute-pcorr
There's no need of installing globally via -g

sachin
  • 1,075
  • 1
  • 7
  • 11