angular-cli icon indicating copy to clipboard operation
angular-cli copied to clipboard

3rd Party Library with Typescript not working for me.

Open aderbas opened this issue 10 years ago • 13 comments

Please provide us with the following information: angular-cli: 1.0.0-beta.11-webpack.2 node: 5.8.0 os: linux x64 Ubuntu 14.04 LTS TypeScript: Version 2.0.0 Angular 2 RC.5

I'm trying to use Leaflet Maps on my app. I installed the leaflet using the "3rd Party Library Installation" described in the documentation.

npm install leaflet --save
npm install @types/leaflet --save-dev

In my editor (Atom) everything is OK with the project, no errors, do the build ng build and gives no error. But when I run the app in the browser I get the error:

EXCEPTION: Error: Uncaught (in promise): EXCEPTION: Error in ./MainViewComponent class MainViewComponent_Host - inline template:0:0
ORIGINAL EXCEPTION: ReferenceError: L is not defined
ORIGINAL STACKTRACE:
ReferenceError: L is not defined
    at new MapService (http://localhost:4200/main.bundle.js:66440:32)
    at DebugAppView._View_MainViewComponent_Host0.createInternal (MainViewComponent.ngfactory.js:20:26)
    at DebugAppView.AppView.create (http://localhost:4200/main.bundle.js:60438:21)
    at DebugAppView.create (http://localhost:4200/main.bundle.js:60651:44)
    at ComponentFactory.create (http://localhost:4200/main.bundle.js:43429:36)
    at ViewContainerRef_.createComponent (http://localhost:4200/main.bundle.js:43998:45)
    at RouterOutlet.activate (http://localhost:4200/main.bundle.js:51268:40)
    at ActivateRoutes.placeComponentIntoOutlet (http://localhost:4200/main.bundle.js:15482:16)
    at ActivateRoutes.activateRoutes (http://localhost:4200/main.bundle.js:15459:22)
    at http://localhost:4200/main.bundle.js:15421:19
ERROR CONTEXT:
[object Object]
browser_adapter.js:84 EXCEPTION: Error: Uncaught (in promise): EXCEPTION: Error in ./MainViewComponent class MainViewComponent_Host - inline template:0:0
ORIGINAL EXCEPTION: ReferenceError: L is not defined
ORIGINAL STACKTRACE:
ReferenceError: L is not defined
    at new MapService (http://localhost:4200/main.bundle.js:66440:32)
    at DebugAppView._View_MainViewComponent_Host0.createInternal (MainViewComponent.ngfactory.js:20:26)
    at DebugAppView.AppView.create (http://localhost:4200/main.bundle.js:60438:21)
    at DebugAppView.create (http://localhost:4200/main.bundle.js:60651:44)
    at ComponentFactory.create (http://localhost:4200/main.bundle.js:43429:36)
    at ViewContainerRef_.createComponent (http://localhost:4200/main.bundle.js:43998:45)
    at RouterOutlet.activate (http://localhost:4200/main.bundle.js:51268:40)
    at ActivateRoutes.placeComponentIntoOutlet (http://localhost:4200/main.bundle.js:15482:16)
    at ActivateRoutes.activateRoutes (http://localhost:4200/main.bundle.js:15459:22)
    at http://localhost:4200/main.bundle.js:15421:19
ERROR CONTEXT:
[object Object]BrowserDomAdapter.logError @ browser_adapter.js:84
browser_adapter.js:84 STACKTRACE:BrowserDomAdapter.logError @ browser_adapter.js:84
browser_adapter.js:84 Error: Uncaught (in promise): EXCEPTION: Error in ./MainViewComponent class MainViewComponent_Host - inline template:0:0
ORIGINAL EXCEPTION: ReferenceError: L is not defined
ORIGINAL STACKTRACE:
ReferenceError: L is not defined
    at new MapService (http://localhost:4200/main.bundle.js:66440:32)
    at DebugAppView._View_MainViewComponent_Host0.createInternal (MainViewComponent.ngfactory.js:20:26)
    at DebugAppView.AppView.create (http://localhost:4200/main.bundle.js:60438:21)
    at DebugAppView.create (http://localhost:4200/main.bundle.js:60651:44)
    at ComponentFactory.create (http://localhost:4200/main.bundle.js:43429:36)
    at ViewContainerRef_.createComponent (http://localhost:4200/main.bundle.js:43998:45)
    at RouterOutlet.activate (http://localhost:4200/main.bundle.js:51268:40)
    at ActivateRoutes.placeComponentIntoOutlet (http://localhost:4200/main.bundle.js:15482:16)
    at ActivateRoutes.activateRoutes (http://localhost:4200/main.bundle.js:15459:22)
    at http://localhost:4200/main.bundle.js:15421:19
ERROR CONTEXT:
[object Object]
    at resolvePromise (http://localhost:4200/polyfills.bundle.js:3647:32)
    at http://localhost:4200/polyfills.bundle.js:3624:14
    at ZoneDelegate.invoke (http://localhost:4200/polyfills.bundle.js:3432:29)
    at Object.NgZoneImpl.inner.inner.fork.onInvoke (http://localhost:4200/main.bundle.js:47898:41)
    at ZoneDelegate.invoke (http://localhost:4200/polyfills.bundle.js:3431:35)
    at Zone.run (http://localhost:4200/polyfills.bundle.js:3325:44)
    at http://localhost:4200/polyfills.bundle.js:3680:58
    at ZoneDelegate.invokeTask (http://localhost:4200/polyfills.bundle.js:3465:38)
    at Object.NgZoneImpl.inner.inner.fork.onInvokeTask (http://localhost:4200/main.bundle.js:47889:41)
    at ZoneDelegate.invokeTask (http://localhost:4200/polyfills.bundle.js:3464:43)

My tsconfig:

{
  "compilerOptions": {
    "declaration": false,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "lib": ["es6", "dom"],
    "mapRoot": "./",
    "module": "es6",
    "moduleResolution": "node",
    "outDir": "../dist/out-tsc",
    "sourceMap": true,
    "target": "es5",
    "typeRoots": [
      "../node_modules/@types"
    ],
    "types": [
      "jasmine",
      "leaflet"
    ]
  }
}

My MainComponent:

import { OnInit, Component } from '@angular/core';
import { MapService }        from '../../services/map.service';

@Component({
  selector: 'm-main-view',
  templateUrl: './main.component.html',
  styles: ['./main.component.css'],
  providers: [MapService]
})
export class MainViewComponent implements OnInit {

  constructor(private mapService: MapService){ }

  ngOnInit(){
    let map = new L.Map('map', {
      zoomControl: false,
      center: new L.LatLng(-7.119651, -34.852692),
      zoom: 12,
      minZoom: 4,
      maxZoom: 19,
      layers: [this.mapService.baseMaps.OpenStreetMap]
    });

    L.control.zoom({ position: 'topright' }).addTo(map);
    L.control.layers(this.mapService.baseMaps).addTo(map);
    L.control.scale().addTo(map);

    this.mapService.map = map;
  }

}

Checked in node_modules/@types/leaflet and all modules are there. I do not know what to do, any idea what can be? Need more information about my project just ask.

aderbas avatar Sep 01 '16 20:09 aderbas

I have a similar issue with socket.io-client, any solution to this problem would be appreciated.

meyerzinn avatar Sep 01 '16 23:09 meyerzinn

@aderbas Add to angular-cli.json file in section "scripts"

"scripts": [ "../node_modules/leaflet/dist/leaflet.js", ],

It's working for me

PavlaQ avatar Sep 02 '16 06:09 PavlaQ

@20zinnm give me url to your problem, propably I know a solution

PavlaQ avatar Sep 02 '16 06:09 PavlaQ

@PavlaQ support apps[0].scripts added in version webpack.3

wjke avatar Sep 02 '16 08:09 wjke

@PavlaQ @wjke I did it, I put the angular-cli.json file, the way it says in the documentation

my angular-cli.json

"apps": [
    {
      "main": "src/main.ts",
      "tsconfig": "src/tsconfig.json",
      "mobile": false,
      "scripts": [
        "../node_modules/leaflet/dist/leaflet.js"
      ],
      "styles": [
        "../node_modules/leaflet/dist/leaflet.css"
      ]
    }
  ],

Still I get the same error. But I'm using webpack.2. I need upgrade to webpack.3?

aderbas avatar Sep 02 '16 12:09 aderbas

@aderbas You have to use

"angular-cli": "1.0.0-beta.11-webpack.8"

PavlaQ avatar Sep 02 '16 12:09 PavlaQ

I updated node to 6.x, angular-cli and webpack and now:

$ ng --version
Cannot find module './models/config'
Error: Cannot find module './models/config'
    at Function.Module._resolveFilename (module.js:455:15)
    at Function.Module._load (module.js:403:25)
    at Module.require (module.js:483:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/mobitrakapp/node_modules/angular-cli/addon/ng2/index.js:4:16)
    at Module._compile (module.js:556:32)
    at Object.Module._extensions..js (module.js:565:10)
    at Module.load (module.js:473:32)
    at tryModuleLoad (module.js:432:12)
    at Function.Module._load (module.js:424:3)

and

$ ng serve
npm start

> [email protected] start /mobitrakapp
> ng serve

Could not start watchman; falling back to NodeWatcher for file system events.
Visit http://ember-cli.com/user-guide/#watchman for more info.
Cannot read property 'source' of undefined
TypeError: Cannot read property 'source' of undefined
    at Object.getWebpackCommonConfig (//mobitrakapp/node_modules/angular-cli/addon/ng2/models/webpack-build-common.ts:127:68)
    at new NgCliWebpackConfig (/mobitrakapp/node_modules/angular-cli/addon/ng2/models/webpack-config.ts:13:30)
    at Class.run (/mobitrakapp/node_modules/angular-cli/addon/ng2/tasks/serve-webpack.ts:19:22)
    at /mobitrakapp/node_modules/angular-cli/addon/ng2/commands/serve.ts:84:26
    at process._tickCallback (internal/process/next_tick.js:103:7)

aderbas avatar Sep 02 '16 13:09 aderbas

@aderbas run ng init and overwrite angular-cli.json . Remember about copy changes in your project because it want overwrite all configuration and main files.

PavlaQ avatar Sep 02 '16 13:09 PavlaQ

@PavlaQ thanks for help. But ng init gets same error

Cannot find module './models/config'
Error: Cannot find module './models/config'
.....

aderbas avatar Sep 02 '16 14:09 aderbas

It seems you have the SystemJS version of Angualar-cli. You need to run npm i -g angular-cli@webpack.

ocarreterom avatar Sep 09 '16 11:09 ocarreterom

Fixed here install typescript 2.0.0 and change angular-cli.json

"scripts": [
    "../node_modules/leaflet/dist/leaflet.js"
 ],
"styles": [
     "../node_modules/leaflet/dist/leaflet.css"
 ]

aderbas avatar Sep 09 '16 11:09 aderbas

Hello, I am facing the same issue with Angular 6. I tried including the leaflet with the command 'npm i leaflet', and also added leaflet.css and leaflet.js file path into angular.json. The angular project runs on 4200 port without any error, but I am not able to see any map on my webpage. Any help will be appreciated.

HetaRZinzuvadia avatar Aug 16 '18 20:08 HetaRZinzuvadia

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.