0

I installed requirejs nuget package and added to my Index.cshtml this:

<script src="~/lib/requirejs/require.js" data-main="/js/scripts/tetromino-client/client.js"></script>

My project structure looks like this:

wwwroot
   |-js
     |-scripts
        |-tetromino-client
           |-client.js
           |-block.js
Views
  |-Home
     |-Index.cshtml

client.js

requirejs(["block"], function (Block) {
    var block = new Block(); // Block is undefined
    console.log(block.value);
});

block.js

function Block() {
    this.value = 50;
}

Requirejs cannot resolve block.js and returns undefined. What did I do wrong?

2
  • Have you exported Block using the RequireJS define syntax? Commented Jun 2, 2017 at 16:22
  • Thank you, I thought that define() was similar to requirejs(). I found a solution. Commented Jun 2, 2017 at 16:33

1 Answer 1

1

What you're missing is a requirejs.config call with baseUrl, telling RequireJS where to look for additional libraries:

requirejs.config({baseUrl: './js/scripts/tetromino-client'}):

requirejs(["block"], function (Block) {
    var block = new Block(); // Block is undefined
    console.log(block.value);
});

And then you need to use define syntax when defining the block module:

define(function () {
    function Block() {
        this.value = 50;
    }

    return Block;
});
Sign up to request clarification or add additional context in comments.

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.