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
define is not defined in nodejs typescript app
提问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.
如果我做错了什么,请纠正我。
回答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-loader
here, once installed you can then require("amd-loader");
in your project.
我想你需要一个amd-loader
here,一旦安装,你就可以require("amd-loader");
在你的项目中。
npm install amd-loader
require("amd-loader");
Below is the link:
以下是链接: