0

I am using angular-cli in angular2 rc1 for development.

I have installed lodash node_module through npm and configured it in systemjs using following:

system.config.ts

/***********************************************************************************************
* User Configuration.
**********************************************************************************************/
/** Map relative paths to URLs. */
const map: any = {
};

/** User packages configuration. */
const packages: any = {
};

////////////////////////////////////////////////////////////////////////////////////////////////
/***********************************************************************************************
 * Everything underneath this line is managed by the CLI.
 **********************************************************************************************/
const barrels: string[] = [
  // Angular specific barrels.
  '@angular/core',
  '@angular/common',
  '@angular/compiler',
  '@angular/http',
  '@angular/router',
  '@angular/platform-browser',
  '@angular/platform-browser-dynamic',
  'ng2-dnd',
  'ng2-bootstrap',
  'moment',
  'lodash',

  // Thirdparty barrels.
  'rxjs',

 // App specific barrels.
 'app',
 'app/shared',     
 /** @cli-barrel */
];

const cliSystemConfigPackages: any = {};
barrels.forEach((barrelName: string) => {
    if(barrelName=='ng2-dnd'){
        cliSystemConfigPackages[barrelName] = { main: 'ng2-dnd' };
    }else if (barrelName == 'ng2-bootstrap') {
        cliSystemConfigPackages[barrelName] = { main: 'ng2-bootstrap' };
    }else if (barrelName == 'lodash') {
        cliSystemConfigPackages[barrelName] = { main: 'lodash' };
    }else if (barrelName == 'moment') {
        cliSystemConfigPackages[barrelName] = { main: 'moment' };
    }else{
        cliSystemConfigPackages[barrelName] = { main: 'index' };
    }

});

/** Type declaration for ambient System. */
declare var System: any;

// Apply the CLI SystemJS configuration.
System.config({
  map: {
    '@angular': 'vendor/@angular',
    'rxjs': 'vendor/rxjs',
    'main': 'main.js',
    'ng2-dnd': 'vendor/ng2-dnd',
    'ng2-bootstrap':'vendor/ng2-bootstrap',
    'moment':'vendor/moment',
    'lodash':'vendor/lodash'
 },
 meta: {
    lodash: { format: 'amd' }
 },  
 packages: cliSystemConfigPackages
});

// Apply the user's configuration.
System.config({ map, packages });

I would just like to note that other node_modules are working correctly i.e. moment,ng2-bootstrap etc. angular-cli-build.js

/* global require, module */

var Angular2App = require('angular-cli/lib/broccoli/angular2-app');

module.exports = function(defaults) {
   return new Angular2App(defaults, {
     vendorNpmFiles: [
       'systemjs/dist/system-polyfills.js',
       'systemjs/dist/system.src.js',
       'zone.js/dist/**/*.+(js|js.map)',
       'es6-shim/es6-shim.js',
       'reflect-metadata/**/*.+(js|js.map)',
       'rxjs/**/*.+(js|js.map)',
       '@angular/**/*.+(js|js.map)',
       'ng2-dnd/**/*.js',
       'ng2-bootstrap/**/*.js',
       'moment/*.js',
       'lodash/*.js'
     ]
   });
 };

after this configuration of lodash node_module, I am importing it from the directory dist\vendors\lodash

in my app.component i am importing it as :

import _ from 'lodash';

But I am getting below error:

Cannot find module 'lodash'

any solutions? thanks in advance

6
  • Have you added below tag in index.html? <script src="cdnjs.cloudflare.com/ajax/libs/lodash.js/4.13.0/…> Commented Jun 9, 2016 at 5:52
  • @Sanket tried adding that,still not working Commented Jun 9, 2016 at 5:56
  • See if you can get some help from this - github.com/jamesemann/ng2-vscode-template Commented Jun 9, 2016 at 6:00
  • Why did you put it in the angular specific list? Commented Jun 9, 2016 at 6:09
  • @brianlmerritt it doesnt matter, its just an array ,moment & ng2-bootstrap are working fine Commented Jun 9, 2016 at 6:12

2 Answers 2

1

I can suggest you a workaround until they get better support for 3rd party libs. It worked for me :)

In your angular-cli-build.json , make sure it remains like this way

module.exports = function(defaults) {
  return new Angular2App(defaults, {
    vendorNpmFiles: [
      ...
      'lodash/**/*.js'
    ]
  });
};

and in your system-config.ts:

/** Map relative paths to URLs. */
 const map: any = {
   'lodash': 'vendor/lodash/lodash.js'
 };

 /** User packages configuration. */
 const packages: any = {
   'lodash': {
     format: 'cjs'
   }
 };

in your src/index.html add this line

 <script src="/vendor/lodash/lodash.js" type="text/javascript"></script>

now in your component where you want to use lodash , write this way

declare var _:any;

@Component({
})
export class YourComponent {
  ngOnInit() {
     console.log(_.chunk(['a', 'b', 'c', 'd'], 2));
  }
}
Sign up to request clarification or add additional context in comments.

4 Comments

I tried the same and is not working U_U In my vendor/lodash folder there are a lot of .js files and not the lodash.js
Did you npm install lodash --save
yes. But not exist lodash.js; there are a lot of files: clone.js, compact.js, concat.js, curry.js, etc.
can you show your screenshot of node_module folder?
0

Try using:

import * as _ from 'lodash';

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.