8

I created a repo in bitbucket which called "angular-lister", the structure of the repo is:enter image description here

Then I created another repo which have the same structure (sort of, cant put picture here and it doesnt really matter).
In this second repo I installed my repo using npm i --save path/to/angular-lister.git and I saw it was added to my package json + it's located under my node_modules folder.

I am trying to import a component from angular-lister (app/app.component) but I am unable to do it.

This is my app.module.ts of the second repo (NOT angular-lister):

import { ListerAppComponent } from 'node_modules/angular-lister/app/app.component'

@NgModule({
    imports:      [
        ...
    ],
    declarations: [
        ...,
        ListerAppComponent
    ],
    bootstrap:    [ ...]
})

and I get the following error:

zone.js:1382 GET http://localhost:3000/node_modules/angular-lister/app/app.component 404 (Not Found)

Why is that, what am I doing wrong here?

EDIT:

content of files in the main project (which is using angular-lister)

package.json:

{
  "name": "angular-project",
  "version": "1.0.0",
  "scripts": {
    "start": "concurrently \"npm run tsc:w\" \"npm run lite\" ",
    "lite": "lite-server",
    "tsc": "tsc",
    "tsc:w": "tsc -w"
  },
  "licenses": [
    {
      "type": "MIT",
      "url": "https://github.com/angular/angular.io/blob/master/LICENSE"
    }
  ],
  "dependencies": {
    "@angular/common": "~2.1.1",
    "@angular/compiler": "~2.1.1",
    "@angular/core": "~2.1.1",
    "@angular/forms": "~2.1.1",
    "@angular/http": "~2.1.1",
    "@angular/platform-browser": "~2.1.1",
    "@angular/platform-browser-dynamic": "~2.1.1",
    "@angular/router": "~3.1.1",
    "@angular/upgrade": "~2.1.1",
    "angular-in-memory-web-api": "~0.1.13",
    "bootstrap": "^3.3.7",
    "core-js": "^2.4.1",
    "angular-lister": "git+https://bitbucket.org/project/angular-lister.git",
    "font-awesome": "^4.7.0",
    "ng2-bootstrap": "^1.1.16",
    "reflect-metadata": "^0.1.8",
    "rxjs": "5.0.0-beta.12",
    "systemjs": "0.19.39",
    "tinymce": "^4.4.3",
    "zone.js": "^0.6.25"
  },
  "devDependencies": {
    "@types/core-js": "^0.9.34",
    "@types/node": "^6.0.45",
    "concurrently": "^3.0.0",
    "lite-server": "^2.2.2",
    "typescript": "^2.0.3"
  }
}

system.config.js:

/**
 * System configuration for Angular samples
 * Adjust as necessary for your application needs.
 */
(function (global) {
    System.config({
        paths: {
            // paths serve as alias
            'npm:': 'node_modules/'
        },
        // map tells the System loader where to look for things
        map: {
            // our app is within the app folder
            app: 'app',
            // angular bundles
            '@angular/core': 'npm:@angular/core/bundles/core.umd.js',
            '@angular/common': 'npm:@angular/common/bundles/common.umd.js',
            '@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js',
            '@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js',
            '@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
            '@angular/http': 'npm:@angular/http/bundles/http.umd.js',
            '@angular/router': 'npm:@angular/router/bundles/router.umd.js',
            '@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js',
            '@angular/upgrade': 'npm:@angular/upgrade/bundles/upgrade.umd.js',
            // other libraries
            'rxjs':                      'npm:rxjs',
            'angular-in-memory-web-api': 'npm:angular-in-memory-web-api/bundles/in-memory-web-api.umd.js',
            'ng2-bootstrap': 'node_modules/ng2-bootstrap/bundles/ng2-bootstrap.umd.js',
            'moment': 'node_modules/moment/moment.js'
        },
        // packages tells the System loader how to load when no filename and/or no extension
        packages: {
            app: {
                main: './main.js',
                defaultExtension: 'js'
            },
            rxjs: {
                defaultExtension: 'js'
            }
        }
    });
})(this);

node_modules/agnular-lister:

enter image description here

Ron
  • 3,975
  • 17
  • 80
  • 130
  • Can you show the contents of 'node_modules/angular-lister/', your systemjs.config.js and the package.json for angular-lister? – JayChase Dec 10 '16 at 01:29
  • @JayChase added what you asked for, this is what I currently have (I've tried Sakuto solution and reverted what I did because it didnt work). – Ron Dec 10 '16 at 11:24
  • I would have to agree with Sakuto that creating a library module is the way forward. I have a post on it (along with links to a sample repo) [here](http://www.usefuldev.com/blog/post/using-ngc-to-build-an-angular-2-library-project). Note this approach uses the [compiler-cli](https://github.com/angular/angular/tree/master/modules/@angular/compiler-cli) which is needed it you want to use AoT. – JayChase Dec 13 '16 at 03:28
  • @JayChase It's just not working. – Ron Dec 17 '16 at 17:14

1 Answers1

4

node_modules should not be there since you are already telling SystemJS to check in that folder. Just edit it by this:

import { ListerAppComponent } from './angular-lister/app/app.component'

Keep in mind that you can't just import the project, you have to create a npm package and export the right thing. this tutorial is a good entrypoint to understand how it works.

LoïcR
  • 4,940
  • 1
  • 34
  • 50
  • when I change it, it says: `http://localhost:3000/angular-lister/app/app.component 404 (Not Found)` do I need to do something in SystemJS? – Ron Dec 02 '16 at 13:50
  • In your node module, what's the structure of your angular-lister folder? – LoïcR Dec 02 '16 at 13:54
  • node_modules/angular-lister/... where `...` is the same as the picture above. If I navigate to `http://localhost:3000/node_modules/angular-lister/app/app.component.js` the file exists.. – Ron Dec 02 '16 at 13:57
  • 1
    You can't just "import" a full project, check this [link](https://medium.com/@OCombe/how-to-publish-a-library-for-angular-2-on-npm-5f48cdabf435#.jkkb3y8qz) to understand how to create a A2 package – LoïcR Dec 02 '16 at 13:59
  • Alright, I will read it and see if it helps. Is this updated enough to understand? I can see it was published in Oct 15 – Ron Dec 02 '16 at 14:02
  • Well, you do not need typings to make it works, nor updating the systemjs.config, otherwise it's fine – LoïcR Dec 02 '16 at 14:04
  • I followed the instuctions - created a file in the root of my project (angular-lister) with export of my component and etc.. but I was unable to use it. is there a live example where I can see and understand what's going on? – Ron Dec 03 '16 at 13:31