TS2307: Cannot find module 'angular2/core' while importing Angular2 on TypeScript

前端 未结 8 1608
庸人自扰
庸人自扰 2021-01-31 03:39

Hi I have a lil bit of Angular 1 background, I am learning Angular 2.

for starting up with Angular 1, only dependency is to add the angular sources either the angu

相关标签:
8条回答
  • 2021-01-31 04:05

    You should import these files:

    <!-- ZonesJS and Reflect-metadata -->
    <script src="node_modules/angular2/bundles/angular2-polyfills.js"></script>
    <!-- SystemJS -->
    <script src="node_modules/systemjs/dist/system.src.js"></script>
    <!-- RxJS (observables) -->
    <script src="node_modules/rxjs/bundles/Rx.js"></script>
    <!-- Main Angular2 bundle -->
    <script src="node_modules/angular2/bundles/angular2.dev.js"></script>
    

    And use this configuration for SystemJS:

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

    I assume that your TypeScript files are located in a app sub folder and the app/boot.ts is the one that contains the call to the bootstrap function.

    0 讨论(0)
  • 2021-01-31 04:11

    first of all answering the question, What is causing TypeScript not to import modules from angualr2? should I configure TypeScript with Angular2? You've imported module loader but not configured it. You don't have to configure TypeScript with Angular2 but the module loader.

    Since the accepted answer is outdated(aimed at a beta release of Angular2),

    A lot of things changed including the module loader in the current Release Candidate of Angular 2 (RC2), This is what works for me,

    The directory structure.

    .                             # project directory
    ├── app                       # main app directory
    │   ├── app.component.ts
    │   └── main.ts
    ├── bs-config.js             
    ├── index.html
    ├── node_modules              # npm package directory
    ├── package.json
    ├── styles.css
    ├── systemjs.config.js
    ├── tsconfig.json
    └── typings.json
    

    The files are.

    app: App directory where the project files reside.
    app.component.ts: App component file
    main.ts: Entry point and bootstrap
    bs-config.js: Lite Server (a dev server based on browsersync) configuration
    index.html: Index page of the application
    node_modules: where the npm packages are installed
    package.json: npm configuration file
    systemjs.config.js: SystemJS configuration
    tsconfig.json: TypeScript Compiler configuration
    typings.json: Type definitions

    Contents of each file.

    1. index.html file

      <html>
        <head>
          <title>Hello Angular 2</title>
          <meta charset="UTF-8">
          <meta name="viewport" content="width=device-width, initial-scale=1">
          <link rel="stylesheet" href="styles.css">
          <!-- 1. Load libraries -->
           <!-- Polyfill(s) for older browsers -->
          <script src="node_modules/core-js/client/shim.min.js"></script>
          <script src="node_modules/zone.js/dist/zone.js"></script>
          <script src="node_modules/reflect-metadata/Reflect.js"></script>
          <script src="node_modules/systemjs/dist/system.src.js"></script>
      
          <!-- 2. Configure SystemJS -->
          <script src="systemjs.config.js"></script>
          <script>
            System.import('app').catch(function(err){ console.error(err); });
          </script>
        </head>
      
        <!-- 3. Display the application -->
        <body>
          <my-app>Loading...</my-app>
        </body>
      </html>
      
    2. package.json

      {
          "name": "hello-angular-2",
          "version": "1.0.0",
          "scripts": {
            "start": "tsc && concurrently \"npm run tsc:w\" \"npm run lite\" ",
            "lite": "lite-server",
            "postinstall": "typings install",
            "tsc": "tsc",
            "tsc:w": "tsc -w",
            "typings": "typings"
          },
          "license": "ISC",
          "dependencies": {
            "@angular/common":  "2.0.0-rc.2",
            "@angular/compiler":  "2.0.0-rc.2",
            "@angular/core":  "2.0.0-rc.2",
            "@angular/http":  "2.0.0-rc.2",
            "@angular/platform-browser":  "2.0.0-rc.2",
            "@angular/platform-browser-dynamic":  "2.0.0-rc.2",
            "@angular/router":  "2.0.0-rc.2",
            "@angular/router-deprecated":  "2.0.0-rc.2",
            "@angular/upgrade":  "2.0.0-rc.2",
            "systemjs": "0.19.27",
            "core-js": "^2.4.0",
            "reflect-metadata": "^0.1.3",
            "rxjs": "5.0.0-beta.6",
            "zone.js": "^0.6.12",
            "angular2-in-memory-web-api": "0.0.12",
            "bootstrap": "^3.3.6"
          },
          "devDependencies": {
            "concurrently": "^2.0.0",
            "lite-server": "^2.2.0",
            "typescript": "^1.8.10",
            "typings":"^1.0.4"
          }
        }
      
    3. systemjs.config.js

      /**
       * System configuration for Angular 2 samples
       * Adjust as necessary for your application needs.
       */
      (function(global) {
        // map tells the System loader where to look for things
        var map = {
          'app':                        'app', // 'dist',
          '@angular':                   'node_modules/@angular',
          'angular2-in-memory-web-api': 'node_modules/angular2-in-memory-web-api',
          'rxjs':                       'node_modules/rxjs'
        };
        // packages tells the System loader how to load when no filename and/or no extension
        var packages = {
          'app':                        { main: 'main.js',  defaultExtension: 'js' },
          'rxjs':                       { defaultExtension: 'js' },
          'angular2-in-memory-web-api': { main: 'index.js', defaultExtension: 'js' },
        };
        var ngPackageNames = [
          'common',
          'compiler',
          'core',
          'http',
          'platform-browser',
          'platform-browser-dynamic',
          'router',
          'router-deprecated',
          'upgrade',
        ];
        // Individual files (~300 requests):
        function packIndex(pkgName) {
          packages['@angular/'+pkgName] = { main: 'index.js', defaultExtension: 'js' };
        }
        // Bundled (~40 requests):
        function packUmd(pkgName) {
          packages['@angular/'+pkgName] = { main: '/bundles/' + pkgName + '.umd.js', defaultExtension: 'js' };
        }
        // Most environments should use UMD; some (Karma) need the individual index files
        var setPackageConfig = System.packageWithIndex ? packIndex : packUmd;
        // Add package entries for angular packages
        ngPackageNames.forEach(setPackageConfig);
        var config = {
          map: map,
          packages: packages
        };
        System.config(config);
      })(this);
      
    4. tsconfig.json

      {
        "compilerOptions": {
          "target": "es5",
          "module": "commonjs",
          "moduleResolution": "node",
          "sourceMap": true,
          "emitDecoratorMetadata": true,
          "experimentalDecorators": true,
          "removeComments": false,
          "noImplicitAny": false
        }
      }
      
    5. typings.json

      {
        "globalDependencies": {
          "core-js": "registry:dt/core-js#0.0.0+20160317120654",
          "jasmine": "registry:dt/jasmine#2.2.0+20160505161446",
          "node": "registry:dt/node#4.0.0+20160509154515"
        }
      }
      
    6. main.ts

      import { bootstrap }    from '@angular/platform-browser-dynamic';
      import { AppComponent } from './app.component';
      bootstrap(AppComponent);
      
    7. app.component.ts

      import { Component } from '@angular/core';
      @Component({
        selector: 'my-app',
        template: '<h1>Hello World from Angular 2</h1>'
      })
      export class AppComponent { }
      


    Installing dependencies

    After saving all the above files, run npm install on a terminal window at the project location, this will install all the dependencies to node_modules directory which may take some time depending on your connection speed.

    Running Developement server

    After installing all the dependencies, run npm start on terminal at the project location. This will run the typescript compiler and the lite-server concurrently, this helps compile/transpile the code and reload the webpage whenever you change the source code.

    Now a new browser window might open. If not, point your favorite browser to **http://127.0.0.1:3000/** or http://localhost:3000/, if everything goes well you will see a page saying,

    Hello World from Angular 2

    That is it!.


    If you don't want the lite-server to open the browser automatically each time you run npm start, add the following to your bs-config.js.

    module.exports = {
        "open": false
    };
    

    code samples from angular.io

    0 讨论(0)
提交回复
热议问题