I'm trying to implement a local library (made for me) but when I try to execute the code the console throws this error:
PS C:\Users\dydie\Documents\Proyecto Sena> node src/index.js
node:internal/modules/cjs/loader:942
throw err;
^
Error: Cannot find module './back js/algorithms.js'
Require stack:
- C:\Users\dydie\Documents\Proyecto Sena\src\back js\utilities.js
- C:\Users\dydie\Documents\Proyecto Sena\src\index.js
at Module._resolveFilename (node:internal/modules/cjs/loader:939:15)
at Module._load (node:internal/modules/cjs/loader:780:27)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object.<anonymous> (C:\Users\dydie\Documents\Proyecto Sena\src\back js\utilities.js:4:19)
at Module._compile (node:internal/modules/cjs/loader:1105:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Module._load (node:internal/modules/cjs/loader:827:12)
at Module.require (node:internal/modules/cjs/loader:1005:19) {
code: 'MODULE_NOT_FOUND',
requireStack: [
'C:\\Users\\dydie\\Documents\\Proyecto Sena\\src\\back js\\utilities.js',
'C:\\Users\\dydie\\Documents\\Proyecto Sena\\src\\index.js'
]
}
Node.js v18.0.0
seems that the error comes from this line:
const utilities = require("./back js/utilities.js");
ironically I before used the same code for other file and it was working (and still working)
what can I do? I tried to delete and install node_modules and it didn't work.
any idea? thanks for your atention
CodePudding user response:
If back js/utilities.js
wants to require algorithms.js
and it's in the same folder as utilities.js
, then it should require('./algorithms.js')
, not require('./back js/algorithms.js')
Because the path for a require is relative to the file that is executing the require
command.
~/index.js
would need require('./back js/algorithms.js')
to directly require ~/back js/algorithms.js
but
~/back js/utilities.js
would only need require('./algorithms.js')
to require ~/back js/algorithms.js
Note that if ~/back js/utilities.js
wrote require('./back js/algorithms.js')
then it would be looking for ~/back js/back js/utilities.js
...which is likely not the intended path.