1

my node app directory sits at /usr/node/express-test/app.js

I followed this thread to set up my environment so that I can have a freelancer come in and install npm packages globally on our server without having to have access to sudo.

How to npm install globally without root

I've checked my NODE_PATH... looks okay to me.

echo $NODE_PATH
/usr/node/.npm-packages/lib/node_modules

However, the error I see when I try to run my app.js appears to be that it cannot find my globally installed express module

app.js
/usr/node/express-test# node app.js
internal/modules/cjs/loader.js:983
  throw err;
  ^

    Error: Cannot find module 'express'
    Require stack:
    - /usr/node/express-test/app.js
        at Function.Module._resolveFilename (internal/modules/cjs/loader.js:980:15)
        at Function.Module._load (internal/modules/cjs/loader.js:862:27)
        at Module.require (internal/modules/cjs/loader.js:1042:19)
        at require (internal/modules/cjs/helpers.js:77:18)
        at Object.<anonymous> (/usr/node/express-test/app.js:2:15)
        at Module._compile (internal/modules/cjs/loader.js:1156:30)
        at Object.Module._extensions..js (internal/modules/cjs/loader.js:1176:10)
        at Module.load (internal/modules/cjs/loader.js:1000:32)
        at Function.Module._load (internal/modules/cjs/loader.js:899:14)
        at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12) {
      code: 'MODULE_NOT_FOUND',
      requireStack: [ '/usr/node/express-test/app.js' ]

}

Ideas to what the problem may be please?

kruddock
  • 47
  • 2
  • 9

1 Answers1

2

My suggestions on this:

  1. First of all, do not install express or any other packages as global put packages in the package.json and run npm install or do npm install --save express to save the packages to local node_modules and list it in package.json, track that package.json & package-lock.json in git so that it can be used in other places, this way if you shift to some new VM you don't need to install packages as global again, you can just do npm install or better npm ci and it's all work.
  2. Secondly, if you still want to use global packages go to /usr/node/.npm-packages/lib/node_modules and see if you can find express folder there
MiKr13
  • 1,297
  • 13
  • 20
  • I can verify that express is in /usr/node/.npm-packages/lib/node_modules and I would prefer to install packages globally.. I'm basically trying to follow this guide [link]( https://makandracards.com/makandra/72209-how-to-install-npm-packages-globally-without-sudo-on-linux) One big difference, I've replaced ${HOME} in the guide with /usr/node as I want this to be the working directory for all node developers... Didn't really see the point of every developer having their own global package repository. – kruddock Apr 17 '20 at 03:50
  • 2
    In the end, I installed the packages locally. Not what I wanted to do, but I was unable to resolve my original problem any other way. – kruddock Apr 20 '20 at 23:10
  • 1
    @kruddock From my experience with Node.js and other languages like Go where packages and modules are used often and portability needs to be achieved I have found that it's best to keep your packages in local unless there is a very very good reason to make it all global. Portability will be much easier, also you can setup very easily, git pull and npm install, that's it. Really happy that the issue is solved still. Thanks for the vote too. Have a great day. – MiKr13 Apr 21 '20 at 05:04