这是我的第一个文件:
var self = this;
var config = {
'confvar': 'configval'
};
我想把这个配置变量放在另一个文件中,因此我在另一个文件中做了如下操作:
conf = require('./conf');
url = conf.config.confvar;
但是它给我一个错误。
TypeError: 无法读取未定义的属性 'confvar'
我该怎么办?
这是我的第一个文件:
var self = this;
var config = {
'confvar': 'configval'
};
我想把这个配置变量放在另一个文件中,因此我在另一个文件中做了如下操作:
conf = require('./conf');
url = conf.config.confvar;
但是它给我一个错误。
TypeError: 无法读取未定义的属性 'confvar'
我该怎么办?
自从Node.js版本8.9.0以来,你也可以使用ECMAScript模块,并具有不同程度的支持。 文档。
--experimental-modules
当以下内容作为初始输入传递给node,或作为ES模块代码中的导入语句引用时,Node.js将把以下内容视为ES模块:
- 以
.mjs
结尾的文件。- 以
.js
结尾且其最近的package.json
父级包含顶级字段"type"
,其值为"module"
的文件。- 作为参数传递给
--eval
或--input-type=module
。
一旦设置完成,您就可以使用import
和export
了。
使用上面的示例,您可以采取两种方法
// 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 }
// 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'
// 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'
// 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}
),但有多个文件时,这非常有用。
假设您有以下文件夹结构:
./src
├── component
│ ├── index.js
│ ├── myComponent.js
│ └── state.js
└── index.js
您可能从store.js和my-component.js中有各种不同的导出,但只想导出其中一些。
import createState from "./state";
export function example(){ };
export default function() {}
export { example as default } from "./myComponent";
export * from "./myComponent"
export * from "./component"
要实现模块的公共访问,需要使用module.exports:
模块导出对象
该对象在当前模块中是共享的,可通过 require() 访问。exports 对象和 module.exports 对象是同一个对象。更多信息请查看 src/node.js 。exports 对象并不是全局变量,而是每个模块本地的。
例如,如果你想要在 sourceFile.js
中将 variableName
的值设置为 "variableValue"
,则可以按以下两种方式之一设置整个 exports 对象:
module.exports = { variableName: "variableValue" };
或者您可以使用以下方式设置单个值:
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
,那么
const variableName = 'variableValue'
module.exports = variableName
const variableName = require('./sourceFile')
import
吗? - user1063287exports.variableName = "variableValue";
而不是 module.exports.variableName = "variableValue";
。哪一种方式更好?为什么有两种方法呢? - Tomiwamodule.exports = { ClientIDUnsplash : 'SuperSecretKey' };
var { ClientIDUnsplash } = require('./FileOne');
这个示例最适合React。
FileTwo.js
中未定义ClientIDUnsplash
)。取而代之的是这个:const ClientIDUnsplash = require('./FileOne');
- jstm