[英]nodejs cannot find local module
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.
我尝试删除并安装 node_modules,但没有用。
any idea?任何的想法? thanks for your atention
谢谢你的关注
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.如果
back js/utilities.js
想要 require algorithms.js
并且它与utilities.js
在同一个文件夹中,那么它应该require('./algorithms.js')
,而不是require('./back js/algorithms.js')
因为 require 的路径是相对于执行require
命令的文件的。
~/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
~/index.js
需要require('./back js/algorithms.js')
直接要求~/back js/algorithms.js
但~/back js/utilities.js
只需要require('./algorithms.js')
要求~/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.请注意,如果
~/back js/utilities.js
写了require('./back js/algorithms.js')
那么它会寻找~/back js/back js/utilities.js
...这可能不是预期的路径。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.