I use Angular2-cli to create two applications: MyFirstApp and MySecondApp . I need MySecondApp import MyFirstApp so that I can reuse the directive / service / component. When I import MyFirstApp , I have the following compilation error: Cannot find module 'MyFirstApp' .
Here's how to reproduce:
$ npm -v 3.9.3 $ node -v v6.2.1 $ npm list | grep 'angular' ├── @angular/ common@2.0.0-rc.1 ├── @angular/ compiler@2.0.0-rc.1 ├── @angular/ core@2.0.0-rc.1 ├── @angular/ http@2.0.0-rc.1 ├── @angular/ platform-browser@2.0.0-rc.1 ├── @angular/ platform-browser-dynamic@2.0.0-rc.1 ├── @angular/ router@2.0.0-rc.1 ├─┬ angular-cli@1.0.0-beta.5 $ ng new MyFirstApp $ cd MyFirstApp $ ng g service MyFirstAppService $ ng build $ cd .. $ ng new MySecondApp $ cd MySecondApp $ npm install ../MyFirstApp --save
Modify src/app/my-second-app.component.ts so it looks like this:
import { Component } from '@angular/core'; import { MyFirstAppService } from 'MyFirstApp'; @Component({ moduleId: module.id, selector: 'MySecondApp', templateUrl: 'mysecondapp.component.html', styleUrls: ['mysecondapp.component.css'] }) export class MySecondAppAppComponent { title = 'MySecondApp works!'; }
I tried many versions of import { MyFirstAppService } from 'MyFirstApp'; (i.e. import { MyFirstAppServiceService } from './../../node_modules/my-first-app/src/app/my-first-app-service.service'; & amp; co) and plays from using system-config.ts (using this and this ) with no luck. I always have Cannot find module 'MyFirstApp' .
Any suggestions?
Edit : By configuring system-config.ts (lines 1 through 14) as follows:
const map: any = { 'my-first-app': 'vendor/my-first-app/app/index.js' }; const packages: any = { 'my-first-app': { format: 'cjs' } };
I can do import { MyFirstAppServiceService } from 'my-first-app/src/app/my-first-app-service.service'; . .../dist/... does not work. This doesn't seem to be the right way to do this, though ... Can't import { MyFirstAppServiceService } from 'my-first-app/my-first-app-service.service' be done?
Note that the resulting vendors/my-first-app/ directory does not contain MyFirstApp code
$
Thanks.
source share