これが私の最初のファイルです:
var self = this;
var config = {
'confvar': 'configval'
};
この構成変数を別のファイルで実行したいので、別のファイルで次のようにしました。
conf = require('./conf');
url = conf.config.confvar;
しかし、エラーが発生します。
TypeError: 未定義のプロパティ 'confvar' を読み取ることができません
私に何ができる?
ベストアンサー1
編集(2020):
以来Node.js バージョン 8.9.0さまざまなレベルのサポートを備えた ECMAScript モジュールを使用することもできます。ドキュメント。
- Node v13.9.0以降では、実験的なモジュールがデフォルトで有効になっています。
- Nodeのバージョンが13.9.0未満の場合は、
--experimental-modules
Node.js は、初期入力としてノードに渡されるとき、または ES モジュール コード内の import ステートメントによって参照されるときに、以下を ES モジュールとして扱います。
- で終わるファイル
.mjs
。.js
最も近い親ファイルに という値を持つpackage.json
最上位フィールドが含まれている場合に で終わるファイル。"type"
"module"
--eval
またはに引数として渡される文字列--input-type=module
。
設定が完了したら、import
と を使用できますexport
。
上記の例を使用すると、2つのアプローチを取ることができます。
./ソースファイル.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}
) が 1 つしかないが、ディレクトリ内に複数のファイルがある場合に便利です。
次のようなフォルダ構造があるとします。
./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/コンポーネント/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 は実際にはグローバルではなく、各モジュールに対してローカルです。
variableName
たとえば、値を"variableValue"
オンにして公開したい場合はsourceFile.js
、エクスポート全体を次のように設定します。
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
これだけであれば
./ソースファイル.js:
const variableName = 'variableValue'
module.exports = variableName
./consumer.js:
const variableName = require('./sourceFile')