0

I have the following code in Visual Studio, in an MVC application;

/scripts/bin/models/ViewModel.ts

export class ViewModel {
  // view model code
}

Now, I have downloaded requirejs, and set the build mode for typescript to AMD type, so that its output looks such as....

define(["require", "exports"], function(require, exports) {

And so on ...

So then I declare my app/config.js file like so;

require.config({
    baseUrl: '/scripts/bin'
});

And I try to load this up, I have requirejs loaded into the scripts, and attempt to call it...

require(['models/ViewModel'], function (viewModel) {
        console.log("test");
});

And I am simply told that it is an invalid call. No other details. The path that it shows is completely correct, too. Is there some kind of additional configuration required? The requirejs documentation is extremely vague about this.

SOLUTION

This turned out to have nothing to do with requirejs, but instead had to do with IIS.

By default, IIS has a rule known as hiddenSegments. It does not allow you to bring in any code from a folder with bin in the path. I simply renamed the folder from bin to something else, and it worked fine.

3 Answers 3

1

Using require.js with TypeScript is a combination of your .html, require.config, module exports and imports. For a step-by-step guide on moving from CommonJs TypeScript to AMD and require.js, have a look here. Have fun.

Sign up to request clarification or add additional context in comments.

1 Comment

This guide helped a bit, though it turns out my problem was completely unrelated to requireJS, will be posting it soon.
1

The TypeScript compiler doesn't have any knowledge of your require.config - so when you use paths relative to that baseUrl they look invalid to the compiler.

Until something is done to bridge that slight mismatch (i.e. make the compiler super-clever so it can look for require.config sections and use them to check paths) it is easier not to set a baseUrl and use the full path in your import statements:

import vm = require('./scripts/bin/models/ViewModel');

1 Comment

Oh no, the require imports within typescript files work, I am talking about trying to use it anywhere else. For instance, in my _Layout.cshtml, I have a line trying to call the view model, and it just tells me the file doesn't exist - it even gives the right path for it.
0

Are you sure that the require call is done with [] and not just

require('models/ViewModel', function (viewModel) {  // this is an error 
        console.log("test");
});

See : http://requirejs.org/docs/errors.html#requireargs

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.