Monday, 24 April 2017

Loading nodejs modules where the modules and application code are in different path

I've installed nodejs in a custom path like /home/user/node, where the executable is /home/user/node/bin/node as follows:

/home/user/node/
               bin/
                  |-- node
                  `-- npm
               etc/
               include/
                  |
                  `-- some .h files

Inside the same path I've installed all my node modules using npm. So the overall structure is like:

/home/user/node/
               bin/
                  |-- node
                  `-- npm
               etc/
               include/
                  |
                  `-- some .h files
               node_modules/
                  |-- socket.io
                  `-- socketio-jwt-auth

I've my application file in a different directory /home/user/app/index.js.

Inside the file index.js I'm loading the modules like require('socketio-jwt-auth');

Now, I

  1. can't use relative path in the require call of index.js
  2. can't move the index.js file to anywhere else
  3. can't symlink the node_module directory inside app directory.

While trying to run the index.js file by /home/user/node/bin/node /home/user/app/index.js, node is throwing an obvious error Error: Cannot find module 'socketio-jwt-auth'.

Is there any way to do it like this ?



via dibyendu

No comments:

Post a Comment