简体   繁体   中英

Can r.js resolve dependency installed with npm?

I've install r.js from npm with npm install requirejs .

I'm expecting this function to produce some output to console, while running it with node main.js :

// main.js
var requirejs = require('requirejs');

requirejs.optimize({
    baseUrl: STATIC_PATH + 'js',
    nodeRequire: require,
    include: ['a', 'b'],
    out: function(text) {
        console.log(text);
    }
});

And modules A and B are defined as following:

// a.js

define(['b'], function($) {
    console.log('A');
});

I know from requirejs docs that it could handle modules installed with npm, so here I'm trying to use jquery that was previously installed with npm install jquery

// b.js

define(['jquery'], function($) {
    console.log('B', $);
});

But when I'm running main.js I've got an error, which states that r.js could not find file named jquery.js .

So basically my question is am I missing something or it isn't possible to make r.js lookup for modules installed via npm?

UPDATE: Here is github repo with MCVE — https://github.com/sintell/rjs_mcve

Require expects the AMD module syntax, but NPM uses CommonJS. You could tell require (using the paths config) where to find the modules, but they still wouldn't be compatible in many cases.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM