Here's how to debug this.
First, we know that the error is MODULE_NOT_FOUND
, which means that you have a require statement that node failed to resolve.
Second, we know that it fails on a relative import, because of the dot in Cannot find module './helpers/isPrime'
.
Third, we take a look at the require stack. The top line of that is AE_Technical_Challenge/routes/challengeRouter.js
. This means that you have a require('./helpers/isPrime')
statement in that file, and the resolution for this statement fails.
Relative imports expect the path to be relative to the current file, not entry-point. In other words, Node joins the directory the current file is in (AE_Technical_Challenge/routes
) together with the relative require path (./helpers/isPrime
). The result is AE_Technical_Challenge/routes/helpers/isPrime
. The file is not there, so it fails.
If the structure of your project is like this:
.
├── routes/
│ └── challengeRouter.js
├── helpers/
│ └── isPrime.js
└── app.js
then inside the router file you want to do require('../helpers/isPrime')
. The ..
takes you from routes
to project root, and then you navigate to the helper file.
Thus, the simplest solution is to just fix your import to correctly reference the isPrime
file. Moreover, if you are using a modern editor like one of JetBrains IDEs or VSCode, they will auto-import everything for you so you don't have to manually type the require path.
If you are really set on requiring other modules as if you were in your project root, you can check the answers to this question for a multitude of options.