14

I am working on nodejs app in typescript in which i have wrote a file server.js as fallow :

import express = require('express');
import mongoose = require('mongoose');


let app=express();
app.set('port', process.env.PORT || 3000); // Set port to 3000 or the provided PORT variable


/**
 * Start app 
 */
app.listen(app.get('port'), function() {
  console.log(`App listening on port ${app.get('port')}!`);
});

i have use the gulp to traspile it which generate js file as

define(["require", "exports", 'express'], function (require, exports, express) {
    var app = express();
    app.set('port', process.env.PORT || 3000); // Set port to 3000 or the provided PORT variable
    /**
     * Start app
     */
    app.listen(app.get('port'), function () {
        console.log("App listening on port " + app.get('port') + "!");
    });
});

//# sourceMappingURL=../map/server.js.map

but when i run this file am getting error define is not define.

Please correct me if i am doing any mistake.

enter image description here

2 Answers 2

24

If you are writing an application that will run purely in Node.js, you should be using "commonjs" modules, not AMD modules, when you compile your TypeScript files.

If you are writing an application that will run both in Node.js and in the browser, you should be using "umd" modules, not AMD modules, when you compile your TypeScript files.

So, you need to change your the configuration object you pass to the Gulp TypeScript compiler to use { module: "commonjs" } or { module: "umd" }.

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

Comments

4

I guess you need an amd-loader here, once installed you can then require("amd-loader"); in your project.

npm install amd-loader
require("amd-loader");

Below is the link:

https://github.com/ajaxorg/node-amd-loader

1 Comment

I have a module that is defined but is not yet loaded (as indicated by its loaded property. How do I wait for it to load?

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.