javascripttypescriptrequirejschutzpah

require.js require a module with index.js


So I'm trying to set up Typescript and Chutzpah for testing purposes. Typescript is set up to output in this format:

define(['require', 'exports', './someModule'], function(require, exports, someModule) {
    //examplecode
});

Which works fine, the problem occurs when someModule is actually a directory with an index.js.

/app
  app.js
  /someModule
    index.js

require.js is unable to resolve someModule in this way and the test fails.

Is there any way to tell require.js that this is a module?


Solution

  • RequireJS won't automatically check for the presence of index.js and load that as your module. You need to tell RequireJS that when you want to load someModule, it should load someModule/index. I'd set a map in my call to require.config:

    require.config({
      [ ... ]
      map: {
        '*': {
            someModule: 'someModule/index',
        }
      },
    });
    

    You have to adjust the name you give there so that it is a path relative to your baseUrl. It's not clear from the information you give in your question what it should be.

    (For the record, there's also a packages setting that you could probably tweak to do what you want but putting something packages says "this is a package", which is not what you appear to have here. So I would not use it for what you are trying to do.)