typescript 在 nodejs 打字稿应用程序中未定义定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34675659/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 03:12:02  来源:igfitidea点击:

define is not defined in nodejs typescript app

javascriptnode.jstypescript

提问by Rhushikesh

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

我正在用打字稿开发 nodejs 应用程序,其中我写了一个文件 server.js 作为休耕:

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

我已经使用 gulp 将它生成为 js 文件

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

在此处输入图片说明

回答by C Snover

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.

如果您正在编写一个纯粹在 Node.js 中运行的应用程序,那么在编译 TypeScript 文件时,您应该使用“commonjs”模块,而不是 AMD 模块。

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.

如果您正在编写一个可以在 Node.js 和浏览器中运行的应用程序,那么在编译 TypeScript 文件时,您应该使用“umd”模块,而不是 AMD 模块。

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

因此,您需要更改传递给 Gulp TypeScript 编译器的配置对象以使用{ module: "commonjs" }{ module: "umd" }

回答by Thalaivar

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

我想你需要一个amd-loaderhere,一旦安装,你就可以require("amd-loader");在你的项目中。

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

Below is the link:

以下是链接:

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

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