我有一个困难的时间试图得到lodash模块导入。我已经使用npm+gulp设置了我的项目,并不断撞击相同的墙。我试过普通的lodash,也试过lodash。

lodash npm包:(在包根文件夹中有一个index.js文件)

import * as _ from 'lodash';    

结果:

error TS2307: Cannot find module 'lodash'.

lodash-es npm包:(在lodash.js的包根文件夹中有一个默认的导出)

import * as _ from 'lodash-es/lodash';

结果:

error TS2307: Cannot find module 'lodash-es'.   

gulp任务和webstorm都报告了相同的问题。

有趣的是,这没有返回错误:

import 'lodash-es/lodash';

... 但是当然没有“_”…

我的tsconfig。json文件:

{
  "compilerOptions": {
    "target": "es5",
    "module": "system",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": false
  },
  "exclude": [
    "node_modules"
  ]
}

我的gulpfile.js:

var gulp = require('gulp'),
    ts = require('gulp-typescript'),
    uglify = require('gulp-uglify'),
    sourcemaps = require('gulp-sourcemaps'),
    tsPath = 'app/**/*.ts';

gulp.task('ts', function () {
    var tscConfig = require('./tsconfig.json');
    
    gulp.src([tsPath])
        .pipe(sourcemaps.init())
        .pipe(ts(tscConfig.compilerOptions))
        .pipe(sourcemaps.write('./../js'));
});

gulp.task('watch', function() {
    gulp.watch([tsPath], ['ts']);
});

gulp.task('default', ['ts', 'watch']);

如果我理解正确,我的tsconfig中的modulerresolution:'node'应该将import语句指向node_modules文件夹,其中安装了lodash和lodash-es。我还尝试了许多不同的导入方法:绝对路径、相对路径,但似乎都不起作用。什么好主意吗?

如果有必要,我可以提供一个小zip文件来说明这个问题。


当前回答

2016年9月26日更新:

正如@Taytay的回答所说,我们现在可以使用几个月前使用的“typings”安装:

npm install --save @types/lodash

以下是支持这个答案的一些参考文献:

https://www.npmjs.com/package/@types/lodash NPM @types org包中的TypeScript类型

如果仍在使用类型安装,请参阅下面(其他人)关于“'——ambient''和'——global " ''的评论。

此外,在新的快速入门中,配置不再在index.html中;它现在在SystemJS .config.ts中(如果使用SystemJS)。

最初的回答:

这在我的mac上是有效的(在按照快速入门安装了Angular 2之后):

sudo npm install typings --global
npm install lodash --save 
typings install lodash --ambient --save

您将发现各种文件受到影响,例如:

/typings/main.d.ts /typings.json /package.json

Angular 2快速入门使用System.js,所以我在index.html的配置中添加了'map',如下所示:

System.config({
    packages: {
      app: {
        format: 'register',
        defaultExtension: 'js'
      }
    },
    map: {
      lodash: 'node_modules/lodash/lodash.js'
    }
  });

然后在我的.ts代码中,我能够做到:

import _ from 'lodash';

console.log('lodash version:', _.VERSION);

2016年年中起编辑:

正如@tibbus提到的,在某些情况下,你需要:

import * as _ from 'lodash';

如果从angular2-seed开始,如果你不想每次都导入,你可以跳过映射和导入步骤,只需取消tools/config/project.config.ts中的lodash行注释。

为了让我的测试与lodash一起工作,我还必须在karma.conf.js中的文件数组中添加一行:

'node_modules/lodash/lodash.js',

其他回答

安装通通终端:

npm install lodash --save
tsd install lodash --save

在index.html中添加路径

<script>
    System.config({
        packages: {
            app: {
                format: 'register',
                defaultExtension: 'js'
            }
        },
        paths: {
            lodash: './node_modules/lodash/lodash.js'
        }
    });
    System.import('app/init').then(null, console.error.bind(console));
</script>

在.ts文件的顶部导入lodash

import * as _ from 'lodash'

我也遇到了同样的问题,但在一个Angular2应用中,这篇文章只是解决了它:https://medium.com/@s_eschweiler/using-external-libraries-with-angular-2-87e06db8e5d1#.p6gra5eli

文章摘要:

npm安装lodash——save 为Lodash tsd安装下划线添加TypeScript定义 包括Script < Script src="node_modules/lodash/index.js"></ Script > .js System.config({ 道路:{ lodash:“。/ node_modules lodash / index.js ' import * as _ from ' lodash ';

希望对你的案子也有帮助

如果不工作后

$ npm install lodash --save 
$ npm install --save-dev @types/lodash

您尝试这样做并导入lodash

typings install lodash --save

我成功地在我的项目中导入了lodash,使用以下命令:

npm install lodash --save
typings install lodash --save

然后我导入它的方式如下:

Import * as _ from 'lodash';

在systemjs.config.js中我定义了这个:

地图:“lodashs: node_modules/lodash/lodash。”

我在Angular 4.0.0上使用preboot/ Angular -webpack,不得不走一条稍微不同的路线。

@Taytay提供的解决方案对我来说很管用:

npm install --save lodash
npm install --save @types/lodash

并使用以下方法将函数导入给定的. ponent.ts文件:

import * as _ from "lodash";

这是因为没有“默认”导出类。我的不同之处在于,我需要找到在第三方库中加载的方式:供应商。坐在那里的Ts:

src/vendor.ts

我的供应商。Ts文件现在看起来是这样的:

import '@angular/platform-browser';
import '@angular/platform-browser-dynamic';
import '@angular/core';
import '@angular/common';
import '@angular/http';
import '@angular/router';

import 'rxjs';
import 'lodash';

// Other vendors for example jQuery, Lodash or Bootstrap
// You can import js, ts, css, sass, ...