Angular2 5分钟安装错误 - 要求未定义

Leo*_*ban 26 javascript angular

我正在做Angular2 5分钟的快速启动.

现在大约一半的教程,我正确设置了以下文件:

  • index.html的,
  • app.component.ts
  • 应用程序/ boot.ts
  • 的package.json
  • tconfig.json

跑了npm start,我收到这个错误:


Uncaught ReferenceError: require is not defined(anonymous function) @ boot.js:1 angular2-polyfills.js:143 Uncaught TypeError: Cannot read property 'split' of undefinedreadMemberExpression @ system.src.js:1456(anonymous function) @ system.src.js:3224(anonymous function) @ system.src.js:3749complete @ system.src.js:2487run @ angular2-polyfills.js:138zoneBoundFn @ angular2-polyfills.js:111


我找到了关于使用es6垫片和我包含的链接<script src="node_modules/es6-shim/es6-shim.js"></script>.

但是我仍然得到 Uncaught ReferenceError: require is not defined错误.


app.component.ts

import {Component} from 'angular2/core';

@Component({
    selector: 'my-app',
    template: '<h1>My First Angular 2 App</h1>'
})
export class AppComponent { }
Run Code Online (Sandbox Code Playgroud)

(应用程序/ boot.ts)

import {bootstrap}    from 'angular2/platform/browser'
import {AppComponent} from './app.component'

bootstrap(AppComponent);
Run Code Online (Sandbox Code Playgroud)

的index.html

<html>

  <head>
    <title>Angular 2 QuickStart</title>

    <!-- 1. Load libraries -->
    <script src="node_modules/es6-shim/es6-shim.js"></script>
    <script src="node_modules/angular2/bundles/angular2-polyfills.js"></script>
    <script src="node_modules/systemjs/dist/system.src.js"></script>
    <script src="node_modules/rxjs/bundles/Rx.js"></script>
    <script src="node_modules/angular2/bundles/angular2.dev.js"></script>

    <!-- 2. Configure SystemJS -->
    <script>
      System.config({
        packages: {        
          app: {
            format: 'register',
            defaultExtension: 'js'
          }
        }
      });
      System.import('app/boot')
            .then(null, console.error.bind(console));
    </script>

  </head>

  <!-- 3. Display the application -->
  <body>
    <my-app>Loading...</my-app>
  </body>

</html>
Run Code Online (Sandbox Code Playgroud)

的package.json

{
  "name": "angular2-quickstart",
  "version": "1.0.0",
  "scripts": {
    "tsc": "tsc",
    "tsc:w": "tsc -w",
    "lite": "lite-server",
    "start": "concurrent \"npm run tsc:w\" \"npm run lite\" "
  },
  "license": "ISC",
  "dependencies": {
    "angular2": "2.0.0-beta.0",
    "systemjs": "0.19.6",
    "es6-promise": "^3.0.2",
    "es6-shim": "^0.33.3",
    "reflect-metadata": "0.1.2",
    "rxjs": "5.0.0-beta.0",
    "zone.js": "0.5.10"
  },
  "devDependencies": {
    "concurrently": "^1.0.0",
    "lite-server": "^1.3.1",
    "typescript": "^1.7.3"
  }
}
Run Code Online (Sandbox Code Playgroud)

tconfig.json

{
  "compilerOptions": {
    "target": "ES5",
    "module": "system",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": false
  },
  "exclude": [
    "node_modules"
  ]
}
Run Code Online (Sandbox Code Playgroud)

已编译的app/boot.js

在此输入图像描述

上次登录 npm start

在此输入图像描述

小智 27

我发现我带来错误的原因是我的tsconfig.json文件中将模块设置为"commonjs" ,将其更改为系统修复它,现在看起来如下所示

{
  "compilerOptions": {
        "target": "es5",
        "module": "system",
        "declaration": false,
        "noImplicitAny": false,
        "removeComments": true,
        "noLib": false,
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "sourceMap": true
    },
  "exclude": [
    "node_modules"
  ]
}
Run Code Online (Sandbox Code Playgroud)

编辑 这个答案是基于Angular 2 beta 17.根据这个答案的时间和角度2经历的快速变化,这可能不再适用了.

  • "module":"system",并不能阻止我收到此错误 (5认同)
  • Upvoted.有太多过时的例子,特别是对于IIS/Visual Studio.我一直在尝试尝试建造任何建筑物.没注意到5分钟的演练使用的是系统而不是commonjs!谢谢. (2认同)

Rei*_*ard 18

我在angular2.beta15版本中遇到了相同的错误.

我通过删除解决了它

format: 'register',
Run Code Online (Sandbox Code Playgroud)

超出index.html

<script>
    System.config({
        packages: {
            app: {
                format: 'register',
                defaultExtension: 'js'
            }
        }
    });
</script>
Run Code Online (Sandbox Code Playgroud)


Leo*_*ban 11

好吧终于让我的'基本'应用程序工作了.

首先我的问题是npm start没有编译我的打字稿.ts文件.

这篇文章中,我在这里找到了答案找不到外部模块'angular2/angular2' - Angular2 w/Typescript

我需要运行npm install -g tsd@latest来更新我的TypeScript定义.在那之后,我需要更新Angular2 的TypeScript定义(TSD)tsd install angular2.

完成此操作后,我仍然从boot.js文件中收到错误.

而不是这个 import {AppComponent} from './app.component'

我需要这样写 import {AppComponent} from '../app.component.js'


现在它有效! https://github.com/leongaban/angular2quickstart

在此输入图像描述


一个恼人的问题是npm start仍然没有自动编译打字稿文件,所以我仍然需要手动编译每个.ts文件tsc app.component.ts --module system