简体   繁体   English

我如何正确要求同一目录中的JS文件?

[英]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_modulesnode_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.jslet 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.

相关问题 如何在同一目录中索取index.js中的内容? - How do I require something from index.js in the same directory? 如何使用javascript :: minify模块缩小目录结构中的所有js文件? - How do i minify all js files within a directory structure using javascript::minify module? 如何在gulp任务中仅排除.min.js文件,同时保留在同一目录中? - How do I exclude only .min.js files in gulp task, while staying in same directory? 您如何组织js文件,以便可以在一个npm包中要求它们? - How do you organize js files so that you can require from them within one npm package? 如何在 js 中使用外部 js 文件? - How do I use external js files within js? Webpack-Require.context-如何在目录中要求除“ _test.js”以外的所有.js文件? - Webpack - Require.context -How to require all .js files except `_test.js` in a directory? 如何在index.js文件中要求javascript模型文件? - How to require javascript model files within the index.js file? 如何将Backbone.js与Require.js(r.js)一起使用,但在优化后会产生2个文件? - How do I use Backbone.js with Require.js (r.js), but resulting in 2 files after I optimize it? 如何确定文件是否在 Node.js 的目录中? - How do I find out whether a file is within a directory with Node.js? Nginx:我想http2_push目录中的所有js和css文件怎么办? - Nginx: I want to http2_push all js and css files in a directory how can I do?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM