【问题标题】:Undefined reference to require and exports while using Typescript in gulp and nodejs在 gulp 和 nodejs 中使用 Typescript 时对 require 和 exports 的未定义引用
【发布时间】:2016-09-12 10:54:33
【问题描述】:

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

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

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

未捕获的 ReferenceError:未定义导出 @animal.ts:1。
未捕获的 ReferenceError: 未定义要求 @cat.ts:1

然后我切换到第一个学习如何从https://www.youtube.com/watch?v=7xOubdqyqaY 设置 gulp 环境

这是我的项目目录结构:

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

当我在根文件夹(打字稿)的 cmd 中运行 gulp 命令时,它会运行所有任务,编译所有 ts 文件,对其进行 lint 处理并使用 browser-sync 提供服务strong> 和 超静态。但是我遇到了同样的错误

未捕获的 ReferenceError:未定义导出 @animal.ts:1。
未捕获的 ReferenceError: 未定义要求 @cat.ts:1

我的文件内容:

animal.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();

cat.ts

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

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

【问题讨论】:

  • 你是否为 systemjs 添加了类型?
  • ...不。但在第二个中,我认为不需要?
  • 在角度课程中,他使用 systemjs 加载文件。但是当我使用它时,它给了我错误无法读取未定义的拆分属性。所以我直接添加了脚本标签来加载是文件。那不是我的问题
  • 你能添加你的 index.html 页面吗?如果 index.html 中有任何内容,还有 systemjs 配置
  • 无法添加更多原始帖子的链接,第一个项目文件:s000.tinyupload.com/index.php?file_id=01079743288297053535

标签: javascript node.js typescript gulp


【解决方案1】:

终于用 Browserify 让它工作了。

步骤here.

【讨论】:

    猜你喜欢
    • 2017-09-25
    • 2017-12-03
    • 2016-09-01
    • 2015-12-21
    • 1970-01-01
    • 2016-10-12
    • 2020-02-03
    • 2017-06-13
    • 1970-01-01
    相关资源
    最近更新 更多