Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: Cannot find module 'lodash' in angular-cli?

OS

Mac OSX El Capitan

Versions

angular-cli: 1.0.0-beta.8 , node: 6.2.0, os: darwin x64

Description

I tried to add lodash library in angular-cli project:

  • npm install --save lodash
  • typings install lodash --global --save

Successfully installed in node_modules. 👍

system-config.ts:

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

/** User packages configuration. */
const packages: any = {
  'lodash': {
    format: 'cjs',
    defaultExtension: 'js',
    main: 'core.js'
  }
};

angular-cli-build.js

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/**/*.+(ts|js|js.map)',
      'rxjs/**/*.+(js|js.map)',
      '@angular/**/*.+(js|js.map)',
      'lodash/**/*.+(js|js.map)'
    ]
  });
};

And then I tried to import in a service in different ways:

  1. import { chunk, intersection, zip } from 'lodash';

    Error: Cannot find module 'lodash'
    
  2. declare var _; const {chunk, intersection, zip} = _;

     Error: Cannot find name 'chunk'
            Cannot find name 'intersection'
            Cannot find name 'zip'
    

I don't found the way to use lodash...

I did something wrong? I miss something? Is there an issue?

Thank you very much,

Aral.


Similar questions (but don't answer my question):

  • Angular2 - Angular-CLI installing lodash - Cannot find module
  • How to add bootstrap to an angular-cli project

Official docs:

  • https://github.com/angular/angular-cli/wiki/3rd-party-libs
like image 785
Aral Roca Avatar asked Jun 26 '16 13:06

Aral Roca


People also ask

Could not find a declaration file for module lodash?

The error "could not find a declaration file for module 'lodash'" occurs when TypeScript cannot find the type declaration for a lodash-related module. To solve the error install the types for the module by running the command from the error message, e.g. npm install -D @types/lodash .

What is lodash npm?

Lodash is a JavaScript library that provides utility functions for common programming tasks using a functional programming paradigm; it builds upon the older underscore. js library. Lodash has several built-in utility functions that make coding in JavaScript easier and cleaner.

How do you use lodash?

Using your command line, install Lodash in your project folder. And then import it at the top of your JavaScript file in which you would like to use it. You're ready to use Lodash! You can now access all of Lodash's functions inside the “_” object.

How do I know what version of lodash I have?

Lodash version const _ = require("lodash"); const ver = _. VERSION; console. log(ver); The example prints the version of the Lodash library.


2 Answers

Change system-config.ts to:

const map: any = {
  'lodash': 'vendor/lodash',
};

const packages: any = {
  'lodash': {
    format: 'cjs',
    defaultExtension: 'js',
    main: 'index.js'
  }
};

And then import it as:

import 'lodash';
declare var _;

Finally use it as:

_.chunk();
like image 159
codef0rmer Avatar answered Sep 20 '22 23:09

codef0rmer


@codef0rmer answer is good, but if you want specific lodash dependencies and not the whole library, here is an answer with example on how to do that: https://stackoverflow.com/a/38317093/1426570

like image 35
Urigo Avatar answered Sep 20 '22 23:09

Urigo