Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

define is not defined in nodejs typescript app

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

like image 619
Rhushikesh Avatar asked Jan 08 '16 11:01

Rhushikesh


2 Answers

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" }.

like image 115
C Snover Avatar answered Nov 20 '22 16:11

C Snover


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

like image 4
Thalaivar Avatar answered Nov 20 '22 14:11

Thalaivar