在gump和nodejs中使用Typescript时,未定义对require和exports的引用

Undefined reference to require and exports while using Typescript in gulp and nodejs

本文关键字:未定义 require 引用 exports Typescript nodejs gump      更新时间:2023-09-26

在了解Typescript之前,我一直用纯javascript编写web应用程序。它是一个很好的javascript超集,如果我编写和编译它,并使用我的WAMP服务器执行它,我不会有任何问题。

但我听说angular2(我知道angular1),我开始用https://www.lynda.com/AngularJS-tutorials/Learn-AngularJS-2-Basics/428058-2.html课程

他使用nodejsgulpgulp-webserver。我和他一样做了精确的设置,但最后我得到了

未捕获引用错误:导出未定义@动物。ts:1。
未捕获引用错误:未定义require@cat.ts:1

然后我开始学习如何从https://www.youtube.com/watch?v=7xOubdqyqaY

这是我的项目目录结构:

typescript/
|--app/
----index.html, load animal.js, cat.js from js/
----|--js, contains all compiles js files( animal.js, cat.js )
-------|--lib, contains all library files
|--node_modules/
|--typescript/, contains my written typescript files 
---|--animal.ts
---|--cat.ts
|--typing/s, contains typescript definition files
|--gulp.config.js
|--gulpfile.js
|--package.json
|--tsconfig.json
|--tslint.json

当我在根文件夹(typescript)的cmd中运行gulp命令时,它会运行所有任务,编译所有ts文件,对其进行lint处理,并使用浏览器同步超静态为其提供服务。然而,我得到了相同的错误

未捕获引用错误:导出未定义@动物。ts:1。
未捕获引用错误:未定义require@cat.ts:1

我的文件内容:

动物.ts

export class Animal {
    constructor( private name: string, private sound: string ) {
    }  
    makeSound() {
        console.log( `${this.name} makes ${this.sound}` );
    }
}
let a: Animal = new Animal( "tiger33", "Gurrrrrr" );
a.makeSound();

类别

import {Animal} from "./animal" 
let b: Animal = new Animal( "Cat", "Mewww" );
b.makeSound();

请告诉我我做错了什么。我在全球安装了nodegulptsc。我需要安装browserifyrequireJS吗?但在视频中,他们没有使用任何这些。感谢

最终使用Browserify使其正常工作。

这里的步骤。