0
이것은 현재 내가 진행하고있는 간단한 예제입니다.npm.load 콜백이 반환 된 후에 만 module.exports 만 반환합니다.
하는 index.js
var config = require('../config.js');
console.log(config.globalModules); //undefined
config.js는 module.exports
객체를 채울 수 있도록 외부 패키지 (npm
)를 사용합니다.
config.js
var npm = require('npm');
var glob = require('glob');
module.exports = {}
// The majority of methods rely on properties which are not set until npm.load has been called.
npm.load(function (er) {
// now i can use npm properties and methods
module.exports.globalModules = glob.sync('*', { cwd: npm.globalDir})
module.exports.localModules = glob.sync('*', { cwd: npm.dir})
});
나는 여기에있는 모든 비동기/동기 콜백 질문을 읽었으며 동기 패키지를 사용하지만 실패하여이 문제를 해결하려고했습니다. sync
과 wait.for
을 사용해 보았지만 var config
은 여전히 빈 개체를 반환합니다.
config.js
이 필수/반환 될 때 var config
일명 (module.exports
)이 완전히 채워지는 것을 어떻게 확인합니까?
불가능합니다. 모듈 아키텍처를 다시 생각해보십시오. 힌트 : jQuery ninja 모드를 좋아하고 모듈을위한 일종의'.ready()'함수가있다. 비동기 함수가 완료된 후에 모듈을 사용할 수 있도록'.init (콜백) '과 같은 것을 내 보냅니다. – slebetman