Javascript node.js 需要来自父文件夹

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42163049/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 00:57:21  来源:igfitidea点击:

node.js require from parent folder

javascriptnode.js

提问by Coder

I have the following structure:

我有以下结构:

-- node_modules
-- websites
---- common
------- config.js
---- testing
------- test.js

Inside config I have some variables set, which are being exported using module.export.

在配置中,我设置了一些变量,这些变量使用 module.export 导出。

I am trying to retrieve those variables when running node test.jsfrom config.js using the following codes:

我正在尝试node test.js使用以下代码从 config.js运行时检索这些变量:

var configData = require('./common/config.js')
var configData = require('../common/config.js')

None of them work. What can I do to retrieve the data from the other folder?

他们都没有工作。我该怎么做才能从另一个文件夹中检索数据?

回答by ibrahim mahrir

var configData = require('./../common/config.js');
  1. ./is testing/

  2. ./../is websites/

  3. ./../common/is websites/common/

  4. ./../common/config.jsis websites/common/config.js

  1. ./testing/

  2. ./../websites/

  3. ./../common/websites/common/

  4. ./../common/config.jswebsites/common/config.js

回答by Dario

from test.js:

来自 test.js:

const configData = require('../common/config');

You can safely omit '.js'.

您可以放心地省略'.js'.

As documentationsay:

正如文档所说:

File Modules

If the exact filename is not found, then Node.js will attempt to load the required filename with the added extensions: .js, .json, and finally .node.

.js files are interpreted as JavaScript text files, and .json files are parsed as JSON text files. .node files are interpreted as compiled addon modules loaded with dlopen.

A required module prefixed with '/' is an absolute path to the file. For example, require('/home/marco/foo.js') will load the file at /home/marco/foo.js.

A required module prefixed with './' is relative to the file calling require(). That is, circle.js must be in the same directory as foo.js for require('./circle') to find it.

Without a leading '/', './', or '../' to indicate a file, the module must either be a core module or is loaded from a node_modules folder.

If the given path does not exist, require() will throw an Error with its code property set to 'MODULE_NOT_FOUND'.

文件模块

如果没有找到确切的文件名,Node.js 将尝试加载所需的文件名,并添加扩展名:.js、.json,最后是 .node。

.js 文件被解释为 JavaScript 文本文件,而 .json 文件被解析为 JSON 文本文件。.node 文件被解释为使用 dlopen 加载的已编译插件模块。

以“/”为前缀的必需模块是文件的绝对路径。例如, require('/home/marco/foo.js') 将加载位于 /home/marco/foo.js 的文件。

以 './' 为前缀的必需模块与调用 require() 的文件相关。也就是说,circle.js 必须与 foo.js 位于同一目录中,以便 require('./circle') 才能找到它。

如果没有前导 '/'、'./' 或 '../' 来指示文件,则该模块必须是核心模块或从 node_modules 文件夹加载

如果给定的路径不存在,require() 将抛出一个错误,其代码属性设置为“MODULE_NOT_FOUND”。

More info about how require()work here.

有关如何在此处require()工作的更多信息。