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


当前回答

我也遇到了同样的问题,但在一个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 ';

希望对你的案子也有帮助

其他回答

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

安装

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

使用

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

如果您使用rollup,我建议使用它而不是lodash,因为它将正确地进行树摇。

请注意npm install——save将在生产代码中培养应用程序所需的任何依赖关系。 至于“类型”,它只被TypeScript要求,而TypeScript最终会转译成JavaScript。因此,您可能不希望在生产代码中使用它们。我建议把它放在项目的devDependencies中,使用

npm install --save-dev @types/lodash

or

npm install -D @types/lodash

(参见Akash的帖子)。顺便说一下,ng2 tuto就是这么做的。

或者,以下是你的包装方式。Json可以是这样的:

{
    "name": "my-project-name",
    "version": "my-project-version",
    "scripts": {whatever scripts you need: start, lite, ...},
    // here comes the interesting part
    "dependencies": {
       "lodash": "^4.17.2"
    }
    "devDependencies": {
        "@types/lodash": "^4.14.40"
    }
}

只是一个小提示

npm的好处是,如果你不确定你正在寻找的依赖项的最新可用版本,你可以先简单地执行npm install——save或——save-dev,它会自动在你的包中为你设置。Json供进一步使用。

尝试>> TSD安装lodash—保存

另一种优雅的解决方案是只获取所需的内容,而不是导入所有的lodash

import {forEach,merge} from "lodash";

然后在代码中使用它

forEach({'a':2,'b':3}, (v,k) => {
   console.log(k);
})

通过npm安装。

$ npm install lodash --save

现在在文件中导入:

$ import * as _ from 'lodash';

ENV:

Angular CLI: 1.6.6 节点:6.11.2 操作系统:darwin x64 角:22 打印稿:2.4.2 webpack: 3.10.0