javascript - 无法在 Gulpfile 中导入 javascript 文件

标签 javascript gulp

我尝试使用 Gulpfile 中另一个 Javascript 文件中的函数,但到目前为止还无法使其工作。

我需要在Gulpfile中访问的文件:

var hello = function(){
   console.log('Hello')
}

以及我在 Gulpfile 中需要它的方式:

var tools = require('./public/js/tools.js'); 
gulp.task('create_subscriptions', function(){
    tools.hello();
});

tools.hello() 不是函数 被触发。

我在这里做错了什么?

编辑

我做到了

module.exports = {
    hello: hello()
};

exports.hello = hello 有什么区别?

最佳答案

您没有从模块中导出任何内容。局部变量不公开,您需要显式地将它们标记为公共(public)。

exports.hello = hello;
<小时/>
hello: hello()

在保存函数的变量后面有()。您调用它并将返回值(不是函数)分配给您的 hello 属性。

关于javascript - 无法在 Gulpfile 中导入 javascript 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40301258/

相关文章:

javascript - PreventDefault 在 Firefox 中不生效

Laravel Elixir - gulp watch 不触发 version()

gulp - 错误 : Cannot find module 'gulp-uglifyjs' with "gulp watch

Javascript:像这样准确地拆分字符串:/kick @Username "reason"但是用户名可以有特殊字符

javascript - 道场图表上的多线图例

javascript - Angular2/Rxjs 嵌套映射,条件在 guard 中

javascript - clsid :D27CDB6E-AE6D-11cf-96B8-444553540000 的相关性是什么

javascript - 一个或多个 $(document).ready

javascript - Gulp 4 - CSS 缩小和重命名

javascript - Gulp EN0ENT 清理任务后丢失目录错误?