这是我的第一个文件:
var self = this;
var config = {
'confvar': 'configval'
};
我希望这个配置变量放在另一个文件中,所以我在另一个文件中完成了这个操作:
conf = require('./conf');
url = conf.config.confvar;
但它给了我一个错误。
TypeError:无法读取未定义的属性“”confvar“”
我能做什么?
发布于 2011-09-30 22:43:50
编辑(2020):
从Node.js版本8.9.0开始,您还可以使用具有不同支持级别的ECMAScript模块。The documentation。
对于节点v13.9.0及更高版本的
--experimental-modules
当将以下内容作为初始输入传递给节点时,或者在由ES模块代码中的导入语句引用时,
Node.js会将以下内容视为ES模块:
当最近的父文件包含一个值为"module"
.
.js
结尾的以package.json
结尾的"type"
文件作为参数传递给--eval
或--print
,或者使用--input-type=module
.标志通过STDIN管道传输到节点
设置好后,就可以使用import
和export
了。
使用上面的示例,您可以采用两种方法
./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:
// 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
您可以从另一个文件重新导出任何内容。当您只有一个出口点(index.{ts|js}
)但目录中有多个文件时,这很有用。
假设你有这样的文件夹结构:
./src
├── component
│ ├── index.js
│ ├── myComponent.js
│ └── state.js
└── index.js
您可以同时从store.js和my-component.js导出各种内容,但只想导出其中的一部分。
./src/component/myComponent.js:
import createState from "./state";
export function example(){ };
./src/component/state.js:
export default function() {}
./src/component/index.js
export { example as default } from "./myComponent";
export * from "./myComponent"
./src/index.js
export * from "./component"
原始答案:
导出
在当前模块的所有实例之间共享并可通过require()访问的对象。exports与module.exports对象相同。有关更多信息,请参阅src/node.js。exports实际上不是全局的,而是每个模块的局部。
例如,如果您希望在sourceFile.js
上使用值"variableValue"
公开variableName
,则可以这样设置整个导出:
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
,那么
./sourceFile.js:
const variableName = 'variableValue'
module.exports = variableName
./consumer.js:
const variableName = require('./sourceFile')
发布于 2019-03-05 03:38:41
文件FileOne.js
module.exports = { ClientIDUnsplash : 'SuperSecretKey' };
文件FileTwo.js
var { ClientIDUnsplash } = require('./FileOne');
这个例子最适合React。
https://stackoverflow.com/questions/7612011
复制相似问题