如何在AMD项目中使用commonJS节点模块?

时间:2016-04-28 18:29:54

标签: node.js requirejs gulp amd r.js

我们在项目中使用AMD模块。我想使用通过npm install安装的第三方commonJS模块。我有一个shim的问题。因此尝试在requireJS构建中使用commonJS模块的另一种策略。

所以,我正在尝试使用here提到的指令在我的AMD版本中声明我的commonJS模块。在这里,我面临着一个不同的问题。虽然我看到r.js能够在gulp进程中加载​​节点模块,但是在执行时它无法加载节点模块

// NOT WORKING. WHY ??
requirejs.optimize(config, function(buildResponse){
  console.log('build response', buildResponse);
}, cb);

这是我的完整gulp文件:

var gulp = require('gulp');
var requirejs = require('requirejs');

config = {
  name: 'src/app',
  baseUrl: ".",
  out: 'dist/app.build.js',
  nodeRequire: require,
  paths: {
      "jquery": "lib/jquery-1.12.0",
      "underscore": "lib/underscore"
  }
};
requirejs.config(config);

gulp.task('requirejs', function (cb) {

// WORKING: r.js is able to load dagre-d3 in commonJS path
console.log('1.5 dagreD3: ' + requirejs('dagre-d3'));


// WORKING: r.js is able to load dagre-d3 in commonJS path
requirejs(['dagre-d3'], function ( dagreD3 ) {
    console.log('2 dagreD3: ' + dagreD3);
});

// NOT WORKING. WHY ??
requirejs.optimize(config, function(buildResponse){
    console.log('build response', buildResponse);
   }, cb);
});


// The default task (called when you run `gulp` from cli)
gulp.task('default', ['requirejs']);

这是我的git repo说明问题。

以下是我在执行gulp时遇到的错误:

[11:30:55] Starting 'requirejs'...
1.5 dagreD3: [object Object]
[11:30:55] 'requirejs' errored after 339 ms
[11:30:55] Error: Error: ENOENT: no such file or directory, open '/Users/akandregula/arun/amd-practice/using_commonJS_module_with_amd/dagre-d3.js'
In module tree:
    src/app
    at Error (native)

    at /Users/akandregula/arun/amd-practice/using_commonJS_module_with_amd/node_modules/requirejs/bin/r.js:25150:19
    at /Users/akandregula/arun/amd-practice/using_commonJS_module_with_amd/node_modules/requirejs/bin/r.js:3056:39
    at /Users/akandregula/arun/amd-practice/using_commonJS_module_with_amd/node_modules/requirejs/bin/r.js:2996:25
    at Function.prim.nextTick (/Users/akandregula/arun/amd-practice/using_commonJS_module_with_amd/node_modules/requirejs/bin/r.js:24901:9)
    at Object.p.errback (/Users/akandregula/arun/amd-practice/using_commonJS_module_with_amd/node_modules/requirejs/bin/r.js:2995:26)
    at Object.p.callback (/Users/akandregula/arun/amd-practice/using_commonJS_module_with_amd/node_modules/requirejs/bin/r.js:2981:23)
    at Object.p.promise.then (/Users/akandregula/arun/amd-practice/using_commonJS_module_with_amd/node_modules/requirejs/bin/r.js:3035:23)
    at build (/Users/akandregula/arun/amd-practice/using_commonJS_module_with_amd/node_modules/requirejs/bin/r.js:25107:12)
    at runBuild (/Users/akandregula/arun/amd-practice/using_commonJS_module_with_amd/node_modules/requirejs/bin/r.js:27115:17)
    at Object.context.execCb (/Users/akandregula/arun/amd-practice/using_commonJS_module_with_amd/node_modules/requirejs/bin/r.js:1943:33)
2 dagreD3: [object Object]

0 个答案:

没有答案