[英]How do I require JS files within the same directory properly?
I'm having trouble understanding how require() works in JS. 我在理解require()如何在JS中工作时遇到了麻烦。
Here's the issue I'm having: 这是我遇到的问题:
My folder structure looks like this: 我的文件夹结构如下所示:
test
->test
->a.js
->b.js
->c.js
And here is the code I have in each file: 这是我在每个文件中拥有的代码:
In c.js
: 在
c.js
:
function c() {
console.log("c")
}
module.exports = c;
In b.js
: 在
b.js
:
let c = require("./c");
function b() {
c();
console.log("b");
}
exports.b = b;
In a.js
: 在
a.js
:
let b = require("./test/b")
When I execute the code in a.js
, it runs fine. 当我在
a.js
执行代码时,它运行良好。
But when I execute the code in b.js
it throws an error: 但是当我在
b.js
执行代码时,会引发错误:
module.js:472
throw err;
^
Error: Cannot find module './c'
at Function.Module._resolveFilename (module.js:470:15)
at Function.Module._load (module.js:418:25)
at Module.require (module.js:498:17)
at require (internal/module.js:20:19)
at [stdin]:3:9
at ContextifyScript.Script.runInThisContext (vm.js:23:33)
at Object.runInThisContext (vm.js:95:38)
at Object.<anonymous> ([stdin]-wrapper:6:22)
at Module._compile (module.js:571:32)
at evalScript (bootstrap_node.js:391:27)
But when I change the contents of b.js
to be: 但是当我将
b.js
的内容更改为:
let c = require("./test/c");
function b() {
c();
console.log("b");
}
module.exports = b;
Now a.js
throws an error: 现在,
a.js
引发错误:
module.js:472
throw err;
^
Error: Cannot find module './test/c'
at Function.Module._resolveFilename (module.js:470:15)
at Function.Module._load (module.js:418:25)
at Module.require (module.js:498:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/Users/sp/Desktop/test/test/b.js:1:71)
at Module._compile (module.js:571:32)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
What am I not understanding? 我不明白什么?
Node looks for a module
object from a file it require()
s. 节点从它的
require()
文件中寻找module
对象。 Specifically looking at module.exports
专门查看
module.exports
// File a.js
const foo = "Hello"
module.exports = foo
// File b.js
const bar = "World"
module.exports = bar
// File index.js
const a = require('./a')
const b = require('./b')
console.log(a, b) //Hello World
You can also export objects 您还可以导出对象
module.exports = { foo, bar }
Which you import like this 您这样导入的
const { foo, bar} = require("./file")
And when you don't specify a location, it looks in node_modules
当您不指定位置时,它会在
node_modules
中node_modules
const express = require('express') // Looks inside node_modules for express
I assume you're running them all from /test/
root directory. 我假设您都是从
/test/
根目录运行它们。
Running a.js
from root: ./test/b
points to /test/test/b
从根目录运行
a.js
: ./test/b
/test/test/b
指向/test/test/b
Running b.js
from root: ./c
points to /test/c
which does not exist. 从根目录运行
b.js
: ./c
/test/c
指向/test/c
,该目录不存在。
When you run a.js
from root, it requires b.js
from ./test/b
which then requires c.js
going the relative path from its own directory via ./c
- that's why it's working if you run a.js
. 当您从根目录运行
a.js
时,它需要b.js
的./test/b
,然后需要c.js
通过./c
从其自己的目录进入相对路径,这就是为什么如果您运行a.js
则它可以工作的原因。
Now running b.js
from upper /test/
directory results in b.js
looking for c.js
in /test/c
instead of /test/test/c
. 现在运行
b.js
从上/test/
目录结果b.js
寻找c.js
在/test/c
,而不是/test/test/c
。
Changing b.js
to let c = require("./test/c");
更改
b.js
以let c = require("./test/c");
leads to the following: 导致以下情况:
a.js
still requires b.js
from ./test/b
aka /test/test/b
. a.js
仍然需要b.js
aka /test/test/b
./test/b
Now b.js
tries to require from ./test/c
pointing to non existent /test/test/test/c
. 现在,
b.js
尝试从./test/c
要求指向不存在的/test/test/test/c
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.