1

node (18.7.0) cannot find globally installed npm (8.18.0) module.

My app.js includes:

let jp = require('jsonpath')

Command lines & outputs:

$ npm install -g jsonpath
…

$ npm -g list
/usr/local/lib
├── @aws-amplify/cli@9.2.1
├── jsonpath@1.1.1
└── npm@8.18.0

$ node app.js                  

node:internal/modules/cjs/loader:959
  throw err;
  ^

Error: Cannot find module 'jsonpath'
Require stack:
- /Users/user/Code/project/js/app.js
    at Module._resolveFilename (node:internal/modules/cjs/loader:956:15)
    at Module._load (node:internal/modules/cjs/loader:804:27)
    at Module.require (node:internal/modules/cjs/loader:1022:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.<anonymous> (/Users/user/Code/project/js/app.js:1:10)
    at Module._compile (node:internal/modules/cjs/loader:1120:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1174:10)
    at Module.load (node:internal/modules/cjs/loader:998:32)
    at Module._load (node:internal/modules/cjs/loader:839:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/Users/user/Code/project/js/app.js'
  ]
}

Node.js v18.7.0
XDR
  • 4,070
  • 3
  • 30
  • 54
  • See [NodeJS require a global module/package](https://stackoverflow.com/questions/15636367/nodejs-require-a-global-module-package). – MikeM Aug 19 '22 at 18:01

1 Answers1

0

Yes, this won't work! Because your app.js will use jsonpath in current project node_modules, not the one you installed globally. You have to install library in current project, just simply run:

npm install jsonpath

Then it will work properly!

Hoàng Huy Khánh
  • 1,071
  • 8
  • 16
  • What's the point of installing globally if it won't be found? How can I use the global installation? – XDR Aug 19 '22 at 17:50
  • As far as I know, installing globally is usually only for library that support Command line interface (CLI) . For example: `npm install -g pm2`, then you can run command like `pm2 start app.js` on your terminal. – Hoàng Huy Khánh Aug 19 '22 at 17:59
  • I do not recommend using global library in a project even if it's possible. How you gonna manage library version across multiple project? Upgrading version may break your app in future, be careful! – Hoàng Huy Khánh Aug 19 '22 at 18:07