我想使用 opening_hours.js在 node.js shell 脚本中。图书馆及其依赖 SunCalc在本地可用:
- js/suncalc.js
- js/opening_hours.js
我想在脚本中使用 opening_hours.js,如下所示:
#!/usr/bin/env node
// TODO: load libraries
var oh = new opening_hours('We 12:00-14:00');
感兴趣的可能是以下 javascript 文件的摘录:
来自 opening_hours.js:
// make the library accessible for the outside world {{{
if (typeof exports === 'object') {
var moment, SunCalc, i18n;
// For Node.js.
SunCalc = root.SunCalc || require('suncalc');
try { // as long as it is an optional dependency
moment = root.moment || require('moment');
} catch (error_pass) { error_pass }
try { // as long as it is an optional dependency
i18n = require('./locales/core');
} catch (error_pass) { error_pass }
module.exports = factory(SunCalc, moment, i18n, holiday_definitions, word_error_correction, lang);
} else {
// For browsers.
root.opening_hours = factory(root.SunCalc, root.moment, root.i18n, holiday_definitions, word_error_correction, lang);
}
//* }}} */
来自 suncalc.js:
// export as AMD module / Node module / browser variable
if (typeof define === 'function' && define.amd) define(SunCalc);
else if (typeof module !== 'undefined') module.exports = SunCalc;
else window.SunCalc = SunCalc;
最佳答案
问题是 opening_hours.js
期望将 SunCalc 安装为 NPM 模块。您需要更新您发布的代码中的 require
语句以指向本地 SunCalc 文件:
// make the library accessible for the outside world {{{
if (typeof exports === 'object') {
var moment, SunCalc, i18n;
// For Node.js.
SunCalc = root.SunCalc || require('./suncalc'); // CHANGED
try { // as long as it is an optional dependency
moment = root.moment || require('moment');
} catch (error_pass) { error_pass }
try { // as long as it is an optional dependency
i18n = require('./locales/core');
} catch (error_pass) { error_pass }
module.exports = factory(SunCalc, moment, i18n, holiday_definitions, word_error_correction, lang);
} else {
// For browsers.
root.opening_hours = factory(root.SunCalc, root.moment, root.i18n, holiday_definitions, word_error_correction, lang);
}
//* }}} */
关于javascript - 如何将 opening_hours.js 包含到 node.js 脚本中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34807929/