2

I have the following Typescript File which i compile with AMD Modules:

import tsmx = module("../TSMX/tsmx");
tsmx.createServer(1337);

which produces the following js:

define(["require", "exports", "../TSMX/tsmx"], function(require, exports, __tsmx__) {
    var tsmx = __tsmx__;

    tsmx.createServer(1337);
})

When i try to load it in Node.js i get the following error:

(function (exports, require, module, __filename, __dirname) { define(["require ^ ReferenceError: define is not defined at Object. (C:\DB\Repo\TSMX\TSMX\TSMXTest\app.js:1:63) at Module._compile (module.js:456:26) at Object.Module._extensions..js (module.js:474:10) at Module.load (module.js:356:32) at Function.Module._load (module.js:312:12) at Function.Module.runMain (module.js:497:10) at startup (node.js:119:16) at node.js:901:3

How to make this work and get "define" defined? I need AMD Modules because i will load some code in the browser.

I come from C# and AS3 where Imports/Namespaces are a lot easier so sorry if this is a amateur question.

EDIT: As recommended by http://requirejs.org/docs/node.html#nodeModules i added to the top of the file:

if (typeof define !== 'function')
{
var define = require('amdefine')(module );
}

define(function (require)
{
var dep = require('dependency');

return function () { };
});

but this gets placed inside the define call by typescript which makes it useless. Then i manually placed it outside of the define call which just resulted in an "Cannot find module 'amdefine'" error.

1
  • Does this help (with the issue described in your edit): stackoverflow.com/a/16214223/1014822? Otherwise why not have a base class with your core functionality and extend it as an amd module for the web and again as commonjs for node? Commented May 6, 2013 at 20:11

1 Answer 1

1

Just an option: You don't need to use AMD on node. Compile your node files separately from your browser files. Use AMD for browser.

If you still want to use AMD on node code this might help: http://nodejs.org/docs/v0.5.0/api/modules.html

I haven't used node yet, but in the browser define / require are defined in a third party library e.g. requirejs : http://requirejs.org/ demo : http://www.youtube.com/watch?v=4AGQpv0MKsA

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

1 Comment

I prefere a single Module type to keep things easy. And CommonJS does not seem suited for the browser. So AMD seems like the way to go

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.