1

When trying to require two folders containing js files, I receive a throw error, returning that neither of the folders could be found. These modules clearly exist so I don't understand how I can't require them. At first I thought this had something to do with the contents of the js files, but if there's an error in the js files, surely the error would be specifically relevant to the js file contents, which it is not.

Instead it simply states that the modules could not be found

enter image description here

enter image description here

enter image description here

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

Error: Cannot find module './training'
Require stack:
- d:\nodejs_scripts\sessions\session1_05.05.20\main.js
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:957:15)
    at Function.Module._load (internal/modules/cjs/loader.js:840:27)
    at Module.require (internal/modules/cjs/loader.js:1019:19)
    at require (internal/modules/cjs/helpers.js:77:18)
    at Object.<anonymous> (d:\nodejs_scripts\sessions\session1_05.05.20\main.js:2:20)
    at Module._compile (internal/modules/cjs/loader.js:1133:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1153:10)
    at Module.load (internal/modules/cjs/loader.js:977:32)
    at Function.Module._load (internal/modules/cjs/loader.js:877:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [ 'd:\\nodejs_scripts\\sessions\\session1_05.05.20\\main.js' ]
}

I debugged both the ./training contents, and the ./serializer content and no errors were found in either. So the error must be occurring when trying to require the folders from main.js. Is there something obvious i'm missing here?

Thanks in advance.

srb633
  • 793
  • 3
  • 8
  • 16
  • does this answer your question? https://stackoverflow.com/questions/5364928/node-js-require-all-files-in-a-folder – kennyvh May 06 '20 at 00:31
  • I don't believe it does. I just tried the top answer. Placed an index.js inside a new folder containing the training and serializer folders. Even when trying to require the serializer and training folders from the new index.js, then requiring the index.js from the main.js. The same throw error occurs. – srb633 May 06 '20 at 00:45

1 Answers1

2

When you try to require() a folder name, you get these rules as described in the doc:

LOAD_AS_DIRECTORY(X)
1. If X/package.json is a file,
   a. Parse X/package.json, and look for "main" field.
   b. If "main" is a falsy value, GOTO 2.
   c. let M = X + (json main field)
   d. LOAD_AS_FILE(M)
   e. LOAD_INDEX(M)
   f. LOAD_INDEX(X) DEPRECATED
   g. THROW "not found"
2. LOAD_INDEX(X)

LOAD_INDEX(X)
1. If X/index.js is a file, load X/index.js as JavaScript text.  STOP
2. If X/index.json is a file, parse X/index.json to a JavaScript object. STOP
3. If X/index.node is a file, load X/index.node as binary addon.  STOP

So, if you're trying to load require('./training') from within main.js, then ./training is a directory so you follow the LOAD_AS_DIRECTORY() rules. It first looks for a package.json file in that directory. Your directory screenshot does not show one. Then it goes to LOAD_INDEX(X) and looks for index.js, index.json or index.node in that directory. You don't show any of those either.

So, the four possible options appear to all be missing from that directory and thus require() gives up and reports an error. To load a directory, you need one of these four options to be in that directory. To recap, the four options are:

package.json
index.js
index.json
index.node

And, if there's a package.json, it has to have a "main" field that tells require() what to load.

jfriend00
  • 683,504
  • 96
  • 985
  • 979