简体   繁体   中英

Cannot find name module in Angular2 app

I'm using angular-cli for running my typescript powered angular2 app. I have an AppComponent defined like this:

import { Component } from '@angular/core';
import { ServersListComponent } from './servers-list/servers-list.component';

@Component({
    moduleId: module.id,
    selector: 'app',
    templateUrl: 'app.component.html',
    styleUrls: ['app.component.css'],
    directives: []
})
export class AppComponent {

}

angular-cli can't compile this file, because it complains with an error mentioned in topic of this question in the line moduleId: module.id .

My tsconfig.json file is defined as below:

{
    "compileOnSave": false,
    "compilerOptions": {
        "declaration": false,
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "mapRoot": "",
        "module": "commonjs",
        "moduleResolution": "node",
        "noEmitOnError": true,
        "noImplicitAny": false,
        "outDir": "../dist/",
        "rootDir": ".",
        "sourceMap": true,
        "target": "es5",
        "inlineSources": true
    },
    "exclude": [
        "node_modules",
        "typings/main",
        "typings/main.d.ts"
    ]
}

To be able module.id , your project must be a commonjs module, ie the module attribute set to commonjs in the tsconfig.json file. Is it the case:

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs", // <-----
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": false
  },
  "exclude": [
    "node_modules",
    "typings/main",
    "typings/main.d.ts"
  ]
}

A possible work-around could be to add a app.d.ts file like this

https://github.com/johnpapa/pbp-a2-ward/blob/010523471e70677ba2493eb615c8e6316e3d4ee8/app/app.d.ts

Update mid 2017

Since angular migrated to using webpack there is no longer a need to declare moduleId as webpack plugins auto handle (add it) module.id in the final bundle.

As of 13.03.2017 Angular has removed all references to moduleId as it is now deprecated due to the inclusion of webpack.

We added a new SystemJS plugin (systemjs-angular-loader.js) to our recommended SystemJS configuration. This plugin dynamically converts "component-relative" paths in templateUrl and styleUrls to "absolute paths" for you.

We strongly encourage you to only write component-relative paths. That is the only form of URL discussed in these docs. You no longer need to write @Component({ moduleId: module.id }), nor should you.

I had the same problem today when creating a first component in a feature folder using the angular-cli. As Maxime said, you can do this without a module id. But you have to use a relative path for your template and your styles, like inside the initially generated app.component:

templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM