rco
rco

Reputation: 355

Cannot find module '@angular/core' '@angular/core' @angular/platform-browser @angular/platform-browser-dynamic

I try use Angular 2.0.0 with JSMP, SystemJS and TS Loader in ASP.NET MVC 5 (not core) app.

Visual Studio complains that find angular modules.

Severity    Code    Description Project File    Line    Suppression State
Error   TS2307  Cannot find module './components/app.component'.    
Error   TS2307  Cannot find module '@angular/core'. 
Error   TS2307  Cannot find module '@angular/core'. 
Error   TS2307  Cannot find module '@angular/platform-browser'. 
Error   TS2307  Cannot find module '@angular/platform-browser-dynamic'.

I install angular via JSPM.

tsconfig.json

{
  "compilerOptions": {
    "target": "es5",
    /* target of the compilation (es5) */
    "module": "system",
    /* System.register([dependencies], function) (in JS)*/
    "moduleResolution": "node",
    /* how module gets resolved (needed for Angular 2)*/
    "emitDecoratorMetadata": true,
    /* needed for decorators */
    "experimentalDecorators": true,
    /* needed for decorators (@Injectable) */
    "noImplicitAny": false
    /* any has to be written explicity*/
  },
  "exclude": [
    /* since compiling these packages could take ages, we want to ignore them*/
    "jspm_packages",
    "node_modules"
  ],
  "compileOnSave": false
  /* on default the compiler will create js files */
}

config.js

System.config({
  baseURL: "/",
  defaultJSExtensions: true,
  transpiler: "babel",
  babelOptions: {
    "optional": [
      "runtime",
      "optimisation.modules.system"
    ]
  },
  typescriptOptions: {
    "tsconfig": true
  },
  paths: {
    "github:*": "jspm_packages/github/*",
    "npm:*": "jspm_packages/npm/*"
  },

  packages: {
    "app": {
      "main": "bootstrap",
      "format": "system",
      "defaultExtensions": "ts",
      "meta": {
        "*.ts": {
          "loader": "ts"
        }
      }
    }
  },

  map: {
    "@angular/common": "npm:@angular/[email protected]",
    "@angular/compiler": "npm:@angular/[email protected]",
    "@angular/core": "npm:@angular/[email protected]",
    "@angular/http": "npm:@angular/[email protected]",
    "@angular/platform-browser": "npm:@angular/[email protected]",
    "@angular/platform-browser-dynamic": "npm:@angular/[email protected]",
    "@angular/router": "npm:@angular/[email protected]",
    "babel": "npm:[email protected]",
    "babel-runtime": "npm:[email protected]",
    "core-js": "npm:[email protected]",
    "es6-shim": "github:es-shims/[email protected]",
    "reflect-metadata": "npm:[email protected]",
    "rxjs": "npm:[email protected]",
    "ts": "github:frankwallis/[email protected]",
    "zone.js": "npm:[email protected]",
    "github:frankwallis/[email protected]": {
      "typescript": "npm:[email protected]"
    },

  }
});

app.module.ts

import { NgModule }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent }   from './components/app.component';
@NgModule({
    imports: [BrowserModule],
    declarations: [AppComponent],
    bootstrap: [AppComponent]
})
export class AppModule { }

index.html

 <script src="../../jspm_packages/npm/[email protected]/client/shim.min.js"></script>
    <script src="../../jspm_packages/npm/[email protected]/dist/zone.min.js"></script>
    <script src="../../jspm_packages/npm/[email protected]/Reflect.js"></script>
    <script src="../../jspm_packages/system.js"></script>
    <script src="../../config.js"></script>
    <script>
            System.config
            ({
                map: {
                    "@@angular/common": "npm:@@angular/[email protected]",
                    "@@angular/compiler": "npm:@@angular/[email protected]",
                    "@@angular/core": "npm:@@angular/[email protected]",
                    "@@angular/http": "npm:@@angular/[email protected]",
                    "@@angular/platform-browser": "npm:@@angular/[email protected]",
                    "@@angular/platform-browser-dynamic": "npm:@@angular/[email protected]",
                    "@@angular/router": "npm:@@angular/[email protected]",
                    "reflect-metadata": "npm:[email protected]"
                },
                transpiler: "ts",
                packages:
                {
                    "app": {
                        "defaultExtension": "ts",
                        "meta": {
                            "*.ts": {
                                "loader": "ts"
                            }
                        }
                    }
                }
            });

            console.log("System.Config Init OK");
    </script>

Upvotes: 0

Views: 2978

Answers (2)

Carlos R Balebona
Carlos R Balebona

Reputation: 1926

Make sure folder 'node_modules' (click show hidden files) is in the right location (in the project's root, sibling with wwwroot). Also check @angular is showing in the project Dependencies folder:

([proj. name]/Dependencies/npm/@angular...).

I was moving files around as I was integrating the quickstart guide into a new ASP.NET Core project (I know yours' not Core, but just in case), and the node_modules folder was misplaced.

hope this helps

Upvotes: 1

Guilherme Meireles
Guilherme Meireles

Reputation: 7919

To use jspm you need typescript 2.0 so that you can use the path configuration and say to typescript where the module is located.

tsconfig.json

{
    "compilerOptions": {
        "target": "es5",
        "module": "system",
        "moduleResolution": "node",
        "sourceMap": true,
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "removeComments": false,
        "noImplicitAny": false,
        "baseUrl": ".",
        "paths": {
            "@angular/core": [
                "jspm_packages/npm/@angular/[email protected]"
            ],
            "@angular/platform-browser-dynamic": [
                "jspm_packages/npm/@angular/[email protected]"
            ],
            "@angular/common": [
                "jspm_packages/npm/@angular/[email protected]"
            ],
            "@angular/compiler": [
                "jspm_packages/npm/@angular/[email protected]"
            ],
            "@angular/forms": [
                "jspm_packages/npm/@angular/[email protected]"
            ],
            "@angular/http": [
                "jspm_packages/npm/@angular/[email protected]"
            ],
            "@angular/platform-browser": [
                "jspm_packages/npm/@angular/[email protected]"
            ],
            "@angular/router": [
                "jspm_packages/npm/@angular/[email protected]"
            ],
            "es6-shim": [
                "jspm_packages/npm/[email protected]"
            ],
            "reflect-metadata": [
                "jspm_packages/npm/[email protected]"
            ],
            "rxjs/*": [
                "jspm_packages/npm/[email protected]/*"
            ],
            "systemjs": [
                "jspm_packages/npm/[email protected]"
            ],
            "zone.js": [
                "jspm_packages/npm/[email protected]"
            ]
        }
    },
    "exclude": [
        "node_modules",
        "jspm_packages"
    ]
}

Upvotes: 1

Related Questions