0

The file structure looks like this:

= (main-folder)
 - package.json
 - ...
 = server
  - index.js
  - ...
 = deploy-abc // new server
  = src
   - index.js
 = src
  - index.js
  - ...

I am trying to install 'deploy-abc' as a module within the main-folder app. So, I ran : yarn add "/var/www/main-folder/deploy-abc". It installed correctly & I can see the 'deploy-abc' dependency listed in package.json.

However, when I try to access the deploy-abc's exported object, I get node error Error: Cannot find module 'deploy-abc'. E.g: In some file in the main folder:

const deployAbc = require("deploy-abc");
console.log(deployAbc); // error

Where am I going wrong?

Kayote
  • 14,579
  • 25
  • 85
  • 144

2 Answers2

2

As per the node docs: https://nodejs.org/api/modules.html#modules_loading_from_node_modules_folders

If the module identifier passed to require() is not a core module, and does not begin with '/', '../', or './', then Node.js starts at the parent directory of the current module, and adds /node_modules, and attempts to load the module from that location.

So, seeing as your module is now in the main folder, how you require it will depend on the relative location. If you are requiring it from say /src/index.js, then you will need:

const deployAbc = require('../deploy-abc')

You also don't need to specify the actual file in this case, as it defaults to index.js. If however the entry file was say, dabc.js or something else, you would need to also specify that in the location.

Matt Way
  • 32,319
  • 10
  • 79
  • 85
  • Thank you for the info, however, for some reason, its not working. It continues to give `Error: Cannot find module "../deployAbc`. The index file is inside `/deploy-abc/src/`, I tried `const abc = require("../deploy-abc/src/")`, however, that didnt work either. The file from which the request is being made is inside 'server' folder. – Kayote May 03 '18 at 13:26
  • Where is the file located that you are trying to require from? And what is the exact path of the file you want to require? – Matt Way May 03 '18 at 22:44
1

You might have to use the exact relative path. For example, const deployAbc = require("../deploy-abc")

Grace
  • 11
  • 2