首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在Node.js中将一个文件中的变量转换为另一个文件

如何在Node.js中将一个文件中的变量转换为另一个文件
EN

Stack Overflow用户
提问于 2011-09-30 22:40:48
回答 2查看 126.1K关注 0票数 71

这是我的第一个文件:

代码语言:javascript
复制
var self = this;
var config = {
    'confvar': 'configval'
};

我希望这个配置变量放在另一个文件中,所以我在另一个文件中完成了这个操作:

代码语言:javascript
复制
conf = require('./conf');
url = conf.config.confvar;

但它给了我一个错误。

TypeError:无法读取未定义的属性“”confvar“”

我能做什么?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-09-30 22:43:50

编辑(2020):

从Node.js版本8.9.0开始,您还可以使用具有不同支持级别的ECMAScript模块。The documentation

对于节点v13.9.0及更高版本的

  • ,默认情况下会启用实验模块对于低于版本13.9.0的节点,请使用--experimental-modules

当将以下内容作为初始输入传递给节点时,或者在由ES模块代码中的导入语句引用时,

Node.js会将以下内容视为ES模块:

当最近的父文件包含一个值为"module".

  • Strings的顶级字段
  • 时,以.js结尾的以package.json结尾的"type"文件作为参数传递给--eval--print,或者使用--input-type=module.

标志通过STDIN管道传输到节点

设置好后,就可以使用importexport了。

使用上面的示例,您可以采用两种方法

./sourceFile.js:

代码语言:javascript
复制
// 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:

代码语言:javascript
复制
// 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:

代码语言:javascript
复制
// 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

代码语言:javascript
复制
// 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

您可以从另一个文件重新导出任何内容。当您只有一个出口点(index.{ts|js})但目录中有多个文件时,这很有用。

假设你有这样的文件夹结构:

代码语言:javascript
复制
./src
├── component
│   ├── index.js
│   ├── myComponent.js
│   └── state.js
└── index.js

您可以同时从store.js和my-component.js导出各种内容,但只想导出其中的一部分。

./src/component/myComponent.js:

代码语言:javascript
复制
import createState from "./state";
export function example(){ };

./src/component/state.js:

代码语言:javascript
复制
export default function() {}

./src/component/index.js

代码语言:javascript
复制
export { example as default } from "./myComponent";
export * from "./myComponent"

./src/index.js

代码语言:javascript
复制
export * from "./component"

原始答案:

您需要module.exports

导出

在当前模块的所有实例之间共享并可通过require()访问的对象。exports与module.exports对象相同。有关更多信息,请参阅src/node.js。exports实际上不是全局的,而是每个模块的局部。

例如,如果您希望在sourceFile.js上使用值"variableValue"公开variableName,则可以这样设置整个导出:

代码语言:javascript
复制
module.exports = { variableName: "variableValue" };

或者,您可以使用以下命令设置单个值:

代码语言:javascript
复制
module.exports.variableName = "variableValue";

要在另一个文件中使用该值,您需要首先对其进行require(...) (使用相对路径):

代码语言:javascript
复制
const sourceFile = require('./sourceFile');
console.log(sourceFile.variableName);

或者,您可以解构它。

代码语言:javascript
复制
const { variableName } = require('./sourceFile');
//            current directory --^
// ../     would be one directory down
// ../../  is two directories down

如果您想从文件中取出的只有variableName,那么

./sourceFile.js:

代码语言:javascript
复制
const variableName = 'variableValue'
module.exports = variableName

./consumer.js:

代码语言:javascript
复制
const variableName = require('./sourceFile')
票数 157
EN

Stack Overflow用户

发布于 2019-03-05 03:38:41

文件FileOne.js

代码语言:javascript
复制
module.exports = { ClientIDUnsplash : 'SuperSecretKey' };

文件FileTwo.js

代码语言:javascript
复制
var { ClientIDUnsplash } = require('./FileOne');

这个例子最适合React。

票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7612011

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档