[英]Cannot find module (a custom module)
I've following folder structure. 我遵循的文件夹结构。
I am trying to access my custom module ( core_programming/Constants.js
) in other files. 我正在尝试在其他文件中访问我的自定义模块(
core_programming/Constants.js
)。
I can access it in routes/index.js
without any issue using following code. 我可以使用以下代码在
routes/index.js
访问它,而不会出现任何问题。
var Constants = require('../core_programming/Constants.js');
But I am getting error when I try to access it inside core_programming/User.js
with following statement. 但是当我尝试使用以下语句在
core_programming/User.js
访问它时出现错误。
var Constants = require('Constants.js');
It gives following error: 它给出以下错误:
module.js:338
throw err;
^
Error: Cannot find module 'Constants.js'
at Function.Module._resolveFilename (module.js:336:15)
at Function.Module._load (module.js:286:25)
at Module.require (module.js:365:17)
at require (module.js:384:17)
at Object.<anonymous> (D:\nodeJsProjects\AutomateBuild\core_programming\User.js:3:18)
at Module._compile (module.js:434:26)
at Object.Module._extensions..js (module.js:452:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Module.require (module.js:365:17)
at require (module.js:384:17)
1 Oct 11:56:35 - [nodemon] app crashed - waiting for file changes before starting...
I've tried different ways for defining path in require
like ../core_programming/Constants.js
and ./core_programming/Constants.js
but nothing works out. 我试过在定义路径不同的方式
require
像../core_programming/Constants.js
和./core_programming/Constants.js
但没有任何工程了。
What is the correct way for loading custom modules from the same directory. 从同一目录加载自定义模块的正确方法是什么?
And, I am on Windows if that helps. 而且,如果有帮助,我将使用Windows。
Try to use: 尝试使用:
var Constants = require('./Constants.js');
This will force Node to figure out you are looking for a relative path and not a package in node_modules
. 这将迫使Node弄清楚您正在寻找相对路径,而不是
node_modules
的软件包。
On a side note, windows paths use \\
, so consider trying it as well: 附带一提,Windows路径使用
\\
,因此请考虑也尝试使用它:
var Constants = require('.\Constants.js');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.