node.js 如何在node.js中从一个文件中获取一个变量到另一个文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7612011/
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
how to get a variable from a file to another file in node.js
提问by XMen
Here is my first file:
这是我的第一个文件:
var self=this;
var config ={
'confvar':'configval'
};
I want this config variable in another file, so what I have done in another file is:
我想在另一个文件中使用这个配置变量,所以我在另一个文件中所做的是:
conf = require('./conf');
url=conf.config.confvar;
but it gives me an error.
但它给了我一个错误。
TypeError: Cannot read property 'confvar' of undefined
Please suggest what can i do?
请建议我能做什么?
回答by Chance
What you need is module.exports
你需要的是module.exports
Exports
An object which is shared between all instances of the current module and made accessible through require(). exports is the same as the module.exports object. See src/node.js for more information. exports isn't actually a global but rather local to each module.
出口
在当前模块的所有实例之间共享并通过 require() 访问的对象。export 与 module.exports 对象相同。有关更多信息,请参阅 src/node.js。export 实际上不是全局的,而是每个模块的局部变量。
For example, if you would like to expose variableNamewith value "variableValue"on sourceFile.jsthen you can either set the entire exports as such:
例如,如果你想揭露variableName与价值"variableValue"上sourceFile.js,那么你可以设置整个出口为这样的:
module.exports = { variableName: "variableValue" };
OR you can set the individual value with:
或者您可以使用以下方法设置单个值:
module.exports.variableName = "variableValue";
To consume that value in another file, you need to require(...)it first (with relative pathing):
要在另一个文件中使用该值,您需要先使用require(...)它(使用相对路径):
const sourceFile = require('./sourceFile');
console.log(sourceFile.variableName);
Alternatively, you can deconstruct it.
或者,您可以解构它。
const { variableName } = require('./sourceFile');
// current directory --^
// ../ would be one directory down
// ../../ is 2 directories down
If all you want out of the file is variableNamethen
如果你想出来的文件variableName,然后
./sourceFile.js:
./sourceFile.js:
const variableName = 'variableValue'
module.exports = variableName
./consumer.js:
./consumer.js:
const variableName = require('./sourceFile')
Edit (2020):
编辑(2020):
Since Node.js version 8.9.0, you can also use ECMAScript Modules with varying levels of support. The docs
因为Node.js version 8.9.0,您还可以使用具有不同支持级别的 ECMAScript 模块。文档
- For Node v13.9.0 and beyond, experimental modules are enabled by default
- For versions of Node less than version 13.9.0, use
--experimental-modules
- 对于 Node v13.9.0 及更高版本,默认启用实验模块
- 对于低于 13.9.0 的 Node 版本,使用
--experimental-modules
Node.js will treat the following as ES modules when passed to node as the initial input, or when referenced by import statements within ES module code:
- Files ending in .mjs.
- Files ending in .js when the nearest parent
package.jsonfile contains a top-level field"type"with a value of"module".- Strings passed in as an argument to --eval or --print, or piped to node via STDIN, with the flag --input-type=module.
当作为初始输入传递给 node 时,或者当 ES 模块代码中的 import 语句引用时,Node.js 会将以下内容视为 ES 模块:
- 以 .mjs 结尾的文件。
- 当最近的父
package.json文件包含"type"值为.js的顶级字段时,以 .js 结尾的文件"module"。- 字符串作为参数传入--eval 或--print,或通过STDIN 管道传输到节点,带有标志--input-type=module。
Once you have it setup, you can use importand export.
设置完成后,您可以使用import和export。
Using the example above, there are two approaches you can take
使用上面的示例,您可以采用两种方法
./sourceFile.js:
./sourceFile.js:
// this is a named export of variableName
export const variableName = 'variableValue'
// alternatively, you could have exported it as a default.
// For sake of explanation, I'm wrapping the variable in an object
// but it is not necessary.
// you can actually omit declaring what variableName is here.
// { variableName } is equivalent to { variableName: variableName } in this case.
export default { variableName: variableName }
./consumer.js:
./consumer.js:
// there are three ways of importing.
// If you need access to a non-default export, then
// you use { nameOfExportedVariable }
import { variableName } from './sourceFile'
console.log(variableName) // 'variableValue'
// Otherwise, you simply provide a local variable name
// for what was exported as default.
import sourceFile from './sourceFile'
console.log(sourceFile.variableName) // 'variableValue'
./sourceFileWithoutDefault.js:
./sourceFileWithoutDefault.js:
// The third way of importing is for situations where there
// isn't a default export but you want to warehouse everything
// under a single variable. Say you have:
export const a = 'A'
export const b = 'B'
./consumer2.js
./consumer2.js
// then you can import all exports under a single variable
// with the usage of * as:
import * as sourceFileWithoutDefault from './sourceFileWithoutDefault'
console.log(sourceFileWithoutDefault.a) // 'A'
console.log(sourceFileWithoutDefault.b) // 'B'
// you can use this approach even if there is a default export:
import * as sourceFile from './sourceFile'
// default exports are under the variable default:
console.log(sourceFile.default) // { variableName: 'variableValue' }
// as well as named exports:
console.log(sourceFile.variableName) // 'variableValue
回答by GlossyLetus
FileOne.js :
FileOne.js :
module.exports = { ClientIDUnsplash : 'SuperSecretKey' };
FileTwo.js :
FileTwo.js :
var { ClientIDUnsplash } = require('./FileOne');
This example works best for ReactJs
这个例子最适合 ReactJs

