Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing Node modules into Angular 2

I am trying to import a node module into Angular 2 but with no avail.

How can this be achieved?

I am trying to import the following module into Angular 2: https://www.npmjs.com/package/countryjs

Do i need to use system.js to register it into the current app:

<!-- 2. Configure SystemJS -->
    <script>
      System.config({
        packages: {
          app: {
            format: 'register',
            defaultExtension: 'js'
          },
          map: {
              'countryjs': 'node_modules/countryjs/lib/countryjs'
          },
          meta: {
              // sets meta for modules within the package
              'vendor/*': {
                  'format': 'global'
              }
          }
        }
      });
      System.import('app/boot')
            .then(null, console.error.bind(console));


    </script>

Then in the ts file, I use:

import {} from "countryjs"

When i use this (below) it gives me an error

import {Country} from "countryjs"

Am I missing something?

like image 973
Kevin Vella Avatar asked Oct 19 '22 13:10

Kevin Vella


1 Answers

First of all sorry for not posting for so long.

I finally found a solution for this. Please find the altered system.js script:

System.config({
                defaultJSExtensions: true,
                packages: {
                    'app': { format: 'register', "defaultExtension": 'js' }
                },
                map: {
                    'angular2': 'node_modules/angular2',
                    'primeng': 'node_modules/primeng',
                    'rxjs': 'node_modules/rxjs',
                    'ng2-uploader': 'node_modules/ng2-uploader/ng2-uploader',
                    'countryjs': 'node_modules/countryjs'
                }
            });
like image 109
Kevin Vella Avatar answered Nov 14 '22 20:11

Kevin Vella