我有一个困难的时间试图得到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文件来说明这个问题。


当前回答

重要的事情先说

NPM安装——保存lodash

npm install -D @types/lodash

加载完整的lodash库

//some_module_file.ts
// Load the full library...
import * as _ from 'lodash' 
// work with whatever lodash functions we want
_.debounce(...) // this is typesafe (as expected)

或者只加载我们要使用的函数

import * as debounce from 'lodash/debounce'
//work with the debounce function directly
debounce(...)   // this too is typesafe (as expected)

更新- 2017年3月

我目前正在使用ES6模块,最近我能够像这样使用lodash:

// the-module.js (IT SHOULD WORK WITH TYPESCRIPT - .ts AS WELL) 
// Load the full library...
import _ from 'lodash' 
// work with whatever lodash functions we want
_.debounce(...) // this is typesafe (as expected)
...

或导入特定的lodash功能:

import debounce from 'lodash/debounce'
//work with the debounce function directly
debounce(...)   // this too is typesafe (as expected)
...

注意-语法中不需要* as的区别

引用:

祝你好运。

其他回答

尝试>> TSD安装lodash—保存

下面是如何做到这一点的Typescript 2.0: (tsd和类型不赞成以下):

$ npm install --save lodash

# This is the new bit here: 
$ npm install --save-dev @types/lodash

然后,在.ts文件中:

:

import * as _ from "lodash";

或者(正如@Naitik所建议的那样):

import _ from "lodash";

我不确定有什么不同。我们使用并更喜欢第一种语法。然而,有些人报告说第一种语法不适合他们,还有人评论说后一种语法与惰性加载webpack模块不兼容。YMMV。

2017年2月27日编辑:

根据下面@Koert, import * as _ from "lodash";是Typescript 2.2.1、lodash 4.17.4和@types/lodash 4.14.53中唯一可用的语法。他说,另一种建议的导入语法给出的错误是“没有默认导出”。

我还为lodash创建了类型,所以现在您实际上可以执行以下操作

安装

npm install lodash-es -S
npm install @types/lodash-es -D

使用

import kebabCase from "lodash-es/kebabCase";
const wings = kebabCase("chickenWings");

如果您使用rollup,我建议使用它而不是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, ...

通过typeings和tsd命令管理类型最终被弃用,取而代之的是通过npm install @types/lodash来使用npm。

然而,我在import语句中“不能找到模块lodash”很长一段时间:

import * as _ from 'lodash';

最终我意识到Typescript只会从node_modules/@types开始版本2加载类型,而我的VsCode语言服务仍然使用1.8,所以编辑器报告错误。

如果你使用的是VSCode,你会想要包含

"typescript.tsdk":  "node_modules/typescript/lib"

在你的VSCode设置。Json文件(用于工作空间设置),并确保通过NPM install typescript@2.0.2——save-dev安装了typescript版本>= 2.0.0

在那之后,我的编辑再也不会抱怨导入语句了。