Tag: 需要

Node.js:我如何将全局variables传递给通过require()插入的文件?

我已经把我的gulpfile.js分成几个文件在一个/gulp gulpfile.js文件夹中,以便更好地组织代码。 但是现在我想将一个variablesdebug (boolean)传递给将会包含gulp命令的行为的文件(最终我将使用命令行参数,但现在我只想使它与variables一起工作)。 我使用的方法是使用我在一个yeoman angular / gulp包中看到的方法,它使用一个名为require-dir的npm模块(它将所有* .js文件加载到/gulp文件夹中,每个文件夹都有一个所设置的吞吐任务)。 gulpfile.js: var gulp = require('gulp'), run = require('run-sequence'), debug = true; require('require-dir')('./gulp'); gulp.task('default', ['build', 'server', 'watch', '…']); 这将加载像… 吞掉/ build.js: var gulp = require('gulp'), plumber = require('gulp-plumber'), …; gulp.task('build', function () { console.log(debug); }); 所以当我运行命令gulp ,运行default任务时,它将加载build.js,然后执行build gulp任务。 但不幸的是,似乎debug返回undefined。 我怎么能得到这个工作? 我正在使用module.exports()和节点的require()方法,但我无法弄清楚如何获取包含文件中的gulp任务来声明,以便它可以从主gulpfile.js文件运行gulpfile.js文件(以所需的顺序)。 任何人可以提供一些援助? 谢谢

JavaScripttesting(摩卡)与'import'js文件

我理解module.export并require module.export : 需要外部js文件进行摩卡testing 虽然只要它是一个模块就可以使用,但我觉得这种方式不方便,因为我现在打算做的是在一个文件中testing代码。 例如,我有一个文件中的代码: app.js 'use strict'; console.log('app.js is running'); var INFINITY = 'INFINITY'; 现在,我想在一个文件中testing这个代码: test.js var expect = require('chai').expect; require('./app.js'); describe('INFINITY', function() { it('INFINITY === "INFINITY"', function() { expect(INFINITY) .to.equal('INFINITY'); }); }); testing代码执行app.js ,所以输出是; app.js is running 然后 ReferenceError: INFINITY is not defined 这不是我所期望的。 我不想使用module.export和写 var app = require('./app.js'); 和 testing代码中的每行都app.INFINITY和app.anyOtherValue 。 […]

覆盖require函数

是否有可能重写全局 require函数,影响它在process级别? 据我所知, require函数是作为包装NodeJS脚本的函数的参数提供的: (function (…, require, __dirname) { // something like this // The wrapped code })(…); 有没有办法修改require函数? (function () { var _require = require; require = function () { console.log("…"); _require.apply(this, arguments); }; })(); 这可能只影响它所在的​​脚本。 我们如何在stream程级别修改它?

如何加载节点模块,就好像当前模块位于其他地方一样?

我有一些nodejs代码在模块中运行。 从这个模块中,我想加载一个位于文件系统中完全不同位置的模块 – 例如,给定path"/another/dir"和模块名称"foo" ,我希望Node充当如果一个运行在/another/dir的模块调用了require("foo") ,而不是我自己的模块。 My code is running here /some/folder/node_modules/mine/my_module.js I have the path "/another/dir/", the string "foo", and want to load this module /another/dir/node_modules/foo/index.js 换句话说, 模块文档引用的过程是“从pathY的模块需要(X)” ,我想为Y指定自己的值 这能做到吗? 如果是这样,怎么样? 如果没有,为什么不呢?

构buildnode.js中需要的库的最佳实践

我有几个实用程序库,其中包含帮助函数,我想加载它们,以便他们可以从控制器使用,我想知道在节点中编码实用程序库的最佳做法是什么。 我有点困惑,因为有几种方法来做到这一点,我不知道什么是最好/更合适/更可靠。 这里有2个选项,但我想知道,如果他们是最好的(例如,我已经看到使用module.exports = exports = function(){}等片段) //option1.js "use strict"; module.exports = function(){ exports.test1 = function(){ console.log('hi I'm test1')}; exports.test2 = function(){ console.log('hi I'm test2')}; return exports; }; //option2.js "use strict"; module.exports = { test1 : function(){ console.log('soy test1')}, test2 : function(){ console.log('soy test2')} }; //test_controller.js /* Requiring helpers in different ways */ var option1 […]

Browserify with require('fs')

我试图在使用fs对象的文件上使用browserify。 当我浏览它时,对require('fs')的调用不会被转换并require返回{} 。 有没有解决方法? 我已经看到了关于stackoverlow和其他地方的一些build议,但没有一个似乎完全实现。 我其实希望创build一个谷歌networking打包的应用程序,使用browserify为我教的课程。 提前致谢。

如何在node.js中需要一个文件,并在请求方法中传递参数,但不传递给模块?

我有一个module.js必须加载; 为了工作需要objectX; 如何将objectX传递给node.js提供的require方法中的module.js? 谢谢 // my module.js objectX.add('abc'); // error: objectX is undefined 我想要一个方法来做到这一点,而不必改变我的所有类,因为会花费很多时间…而且它们对客户端来说具有良好的性能。 (我混合clientfiles与serverfiles ***)

在Node.js中,当我需要时创build一个新对象吗?

所以,我不确定的是那个。 如果在ModuleA中 ,我有: var mongoose = require('mongoose'); mongoose.connect(pathA); 在ModuleB中 ,我有: var mongoose = require('mongoose'); mongoose.connect(pathB); 在主程序中,我有: var mA = require('./moduleA.js'), mB = require('./moduleB.js'); 所以,当我运行主程序时,我想我会创build两个mongoose“实例”一个连接到pathA,一个连接到pathB,是吗? 另外,在模块B中,在连接到pathB之前,它是连接到pathA还是没有连接? 谢谢。

获得闭包编译器和Node.js发挥很好

有没有使用node.js和闭包编译器(简称CC)的项目? CC的官方build议是一起编译应用程序的所有代码,但是当我编译一些包含require("./MyLib.js")简单node.js代码时,该行直接放入输出中,在这方面没有任何意义。 我看到几个选项: 将整个应用程序编码为单个文件。 通过避免它解决了这个问题,但是对维护不利。 假设所有文件在执行之前都会被连接起来。 这又避免了这个问题,但是使得实现一个未编译的debugging模式变得更加困难。 我希望CC能够“理解”node.js require()函数,但是如果不编辑编译器本身,可能无法做到这一点。

Node.js – 检查模块是否安装,而实际上并不需要它

在运行之前,我需要检查是否安装了“mocha”。 我想出了以下代码: try { var mocha = require("mocha"); } catch(e) { console.error(e.message); console.error("Mocha is probably not found. Try running `npm install mocha`."); process.exit(e.code); } 我不喜欢这个想法来捕捉exception。 有没有更好的办法?