requirejsamdjs-amd

How get access to amd module from next module during define


I have this working code.

define(['https://cdnjs.cloudflare.com/ajax/libs/axios/0.19.0/axios.min.js'],function(axios) { 
    axios.post('/user');
});

But I want to use axios inside next module r.js

define([
    'https://cdnjs.cloudflare.com/ajax/libs/axios/0.19.0/axios.min.js',
    'https://example.com/r.js], function(axios) { 
    return r; 
}); 

But axios is undefined.

r.js

var r = function () {
  console.log('worked') // worked
  axios.post('/user'); // axios undefined
}

Is it possible? I have not access to requre.js config :-(


Solution

  • What your code is doing now:

    First solution for your problem is to rewrite r as amd module and specify axios as its dependency.

    Second solution is to specify axios as r's dependency via shim.