Define not defined in nodejs typescript app

I am working on a nodejs application in typescript in which I wrote the server.js file 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 use gulp for traspile which generates 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, then getting the definition of the error is not detected.

Please correct me if I am wrong.

enter image description here

+6
source share
2 answers

If you are writing an application that will run exclusively in Node.js, when compiling TypeScript files, you should use the "commonjs" modules, not the AMD modules.

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

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

+7
source

I think you need amd-loader here, after installation you can require("amd-loader"); in his project.

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

Below is the link:

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

+1
source

All Articles