如何从一个文件中获取变量到node.js中的另一个文件
这是我的第一个文件:
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;
但这给我一个错误.
TypeError: Cannot read property 'confvar' of undefined
请提出我该怎么办?
您需要的是 module.exports
导出
在当前模块的所有实例之间共享的对象 并通过require()进行访问.出口与 module.exports对象.有关更多信息,请参见src/node.js.出口商品 实际上不是每个模块的全局变量,而是局部模块.
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.
例如,如果要在sourceFile.js
上使用值为"variableValue"
的variableName
公开,则可以这样设置整个导出:
For example, if you would like to expose variableName
with value "variableValue"
on sourceFile.js
then you can either set the entire exports as such:
module.exports = { variableName: "variableValue" };
或者您可以使用以下方法设置单个值:
OR you can set the individual value with:
module.exports.variableName = "variableValue";
要在另一个文件中使用该值,则需要先require(...)
(具有相对路径):
To consume that value in another file, you need to require(...)
it first (with relative pathing):
const sourceFile = require('./sourceFile');
console.log(sourceFile.variableName);
或者,您可以对其进行解构.
Alternatively, you can deconstruct it.
const { variableName } = require('./sourceFile');
如果要从文件中删除的全部是variableName
,则
If all you want out of the file is variableName
then
const variableName = 'variableValue'
module.exports = variableName
./consumer.js:
const variableName = require('./sourceFile')
编辑(2020):
自Node.js version 8.9.0
起,您还可以使用具有不同支持级别的ECMAScript模块. 文档
Edit (2020):
Since Node.js version 8.9.0
, you can also use ECMAScript Modules with varying levels of support. The docs
- 对于Node v13.9.0及更高版本,默认情况下启用实验模块
- 对于低于13.9.0版本的Node版本,请使用
--experimental-modules
在作为初始输入传递给节点或被ES模块代码中的import语句引用时,Node.js会将以下内容视为ES模块:
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:
- 以.mjs结尾的文件.
- 当最近的父级
package.json
文件包含值为"module"
的顶级字段"type"
时,文件以.js结尾. - 作为参数传递给--eval或--print的字符串,或通过带有标志--input-type = module的STDIN通过管道传递给节点的信号.
- Files ending in .mjs.
- Files ending in .js when the nearest parent
package.json
file 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.
设置完成后,就可以使用import
和export
.
Once you have it setup, you can use import
and export
.
使用上面的示例,您可以采用两种方法
Using the example above, there are two approaches you can take
// 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:
// 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:
// 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
// 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