error on angular 2 offline template compilation - @angular/http has no exported member HTTP_PROVIDERS

1.3k Views Asked by At

I am currently setting HTTP_PROVIDERS at the app component level. I see the error when running the compiler via ./node_modules/.bin/ngc.

The full error when running the compiler is

Error: Error at /Users/girishnanda/Documents/offlinecompiler/src/app.component.ngfactory.ts:47:33: Module '"/Users/girishnanda/Documents/offlinecompiler/node_modules/@angular/http/http"' has no exported member 'HTTP_PROVIDERS'.
Error at /Users/girishnanda/Documents/offlinecompiler/src/app.component.ngfactory.ts:51:38: Module '"/Users/girishnanda/Documents/offlinecompiler/node_modules/@angular/http/http"' has no exported member 'HTTP_PROVIDERS'.
Error at /Users/girishnanda/Documents/offlinecompiler/src/app.component.ngfactory.ts:53:77: Cannot use 'new' with an expression whose type lacks a call or construct signature.
    at check (/Users/girishnanda/Documents/offlinecompiler/node_modules/@angular/compiler-cli/tsc.js:32:15)
    at Tsc.typeCheck (/Users/girishnanda/Documents/offlinecompiler/node_modules/@angular/compiler-cli/tsc.js:67:9)
    at /Users/girishnanda/Documents/offlinecompiler/node_modules/@angular/compiler-cli/main.js:39:23
    at process._tickCallback (node.js:406:9)
    at Function.Module.runMain (module.js:449:11)
    at startup (node.js:141:18)
    at node.js:933:3
Compilation failed

App component where I am setting the HTTP_PROVIDERS

import {Component} from '@angular/core';
import {HTTP_PROVIDERS} from '@angular/http';
@Component({
  selector: 'app',
  template: `test
               `,
  providers: [HTTP_PROVIDERS]
})
export class AppComponent {
  constructor() { }
}

Versions of Compiler, Compile-cli, typescript and @angular/http used

"@angular/compiler": "2.0.0-rc.1",
"@angular/compiler-cli": "0.2.0",
"@angular/http": "2.0.0-rc.1",
"typescript": "^1.9.0-dev.20160605-1.0"

Tsconfig.json

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

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"
        }
    }

If I don't include the HTTP_PROVIDERS, the compile runs successfully with no errors. Any guidance on what I might need to change so that I can compile with HTTP_PROVIDERS would be helpful.

In reply to the comment of trying to add the provider on bootstrap. This is my main.ts. I haven't yet figured figure out how to add additional providers. This is the base example with the offline compiler.

import {coreBootstrap, ReflectiveInjector, enableProdMode} from '@angular/core';
import {browserPlatform, BROWSER_APP_COMMON_PROVIDERS, BROWSER_APP_STATIC_PROVIDERS} from '@angular/platform-browser';
import {AppComponentNgFactory} from './app.component.ngfactory';
import {AppComponent} from './app.component';

enableProdMode();

const appInjector =
    ReflectiveInjector
    .resolveAndCreate(BROWSER_APP_COMMON_PROVIDERS, browserPlatform().injector);
coreBootstrap(appInjector, AppComponentNgFactory);
1

There are 1 best solutions below

0
On

This works for me.

const appInjector =
    ReflectiveInjector
    .resolveAndCreate([
      BROWSER_APP_PROVIDERS,
      HTTP_PROVIDERS
    ], browserPlatform().injector);
coreBootstrap(someComponentNgFactory, appInjector);