这是我的第一份文件:

var self = this;
var config = {
    'confvar': 'configval'
};

我想把这个配置变量放在另一个文件中,所以我在另一个文件中这样做了:

conf = require('./conf');
url = conf.config.confvar;

但这给了我一个错误.

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

我能做什么?

推荐答案

编辑(2020年):

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

  • 对于 node v13.9.0及更高版本,实验模块默认启用
  • 对于版本低于13.9.0的 node 版本,请使用--experimental-modules

node .当js在ES中被引用时,将以下语句视为输入:

  • 文件以.mjs结尾.
  • 当最近的父文件package.json包含值为"module"的顶级字段"type"时,文件以.js结尾.
  • 字符串作为参数传入--eval--print,或通过STDIN传输到 node ,带有标志--input-type=module.

一旦设置好,就可以使用importexport.

通过上面的例子,你可以采取两种方法

/源文件.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 } 

/消费者.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'

/withsource默认值.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'

/消费者2.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

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

假设您有以下文件夹 struct :

./src
├── component
│   ├── index.js
│   ├── myComponent.js
│   └── state.js
└── index.js

你们可以从这两家store 购买各种出口商品.js和我的组件.但只想导出其中的一部分.

./src/component/myComponent.js:

import createState from "./state";
export function example(){ };

./src/组件/状态.js:

export default function() {}

./src/组件/索引.js

export { example as default } from "./myComponent";
export * from "./myComponent"

/src/index.js

export * from "./component"

原始答案:

你需要module.exports:

出口

在当前模块的所有实例之间共享的对象

例如,如果希望在sourceFile.js上显示值为"variableValue"variableName,则可以将整个导出设置为:

module.exports = { variableName: "variableValue" };

Or您可以使用以下选项设置单个值:

module.exports.variableName = "variableValue";

要在另一个文件中使用该值,需要先将其设置为require(...)(使用相对路径):

const sourceFile = require('./sourceFile');
console.log(sourceFile.variableName);

或者,你可以解构它.

const { variableName } = require('./sourceFile');
//            current directory --^
// ../     would be one directory down
// ../../  is two directories down

如果你只想从文件中得到variableName,那么

/源文件.js:

const variableName = 'variableValue'
module.exports = variableName

/消费者.js:

const variableName = require('./sourceFile')

Node.js相关问答推荐

nest js控制器方法调用两次

如何将信号从终端窗口发送到运行在Raspberry Pi上的Puppeteer/Node.js上的webscraper

在我的Next.js应用程序中没有正确设置Process.env.NODE_ENV

如何在 ElectronJS 中播放音频文件

函数声明中的异步在没有等待的函数中做什么?

为什么即使数据库确实更新了,此 POST 也无法解析并返回 200 代码?

表达 js 错误处理程序在第一个之后被忽略

我应该转译我的 TypeScript 应用程序吗?

如何解决未调用 Express 错误处理程序的问题

在mongodb中只查询整数

nuxt:在 docker 镜像中找不到

在 Express.js 中迭代子文档数组

我怎样才能让`git`失败而不是要求提供凭据

socket.io 发出回调合适吗?

如何运行用 TypeScript 编写的 Mocha 测试?

webpack css-loader 在外部样式表的 url() 引用中找不到图像

如何使用 Node.js 在服务器端管理多个 JS 文件

在 Node.js 中使用公钥加密数据

Google Firebase 错误(函数返回未定义、预期的 Promise 或值)

如何调试 Gulp 任务?