10

Given the following directory structure:

{project}/
  |-- node_modules/
  |    |-- lodash
  |-- src/
  |    |-- index.ts
  |-- lib/ (output)
  |    |-- index.js
  |    |-- index.d.ts
  |-- package.json
  |-- tsconfig.json

Whilst the built output functions properly; the tsc command complains that it cannot resolve the lodash module when I use any of the following:

import _ from "lodash";
import _ = require("lodash");
import * as _ from "lodash";

Inside my 'tsconfig.json' file I have included the following things:

...

"target": "es6",
"sourceMap": true,
"module": "commonjs",
"moduleResolution": "node",

...

But despite this it's still not finding any of the modules that are installed using npm.

Am I missing something that is required to make TypeScript find these modules?

I realize that without a TypeScript definition file TypeScript is unable to provide additional type checks; however, surely these should just default to the any type right?

4

1 回答 1

7

由于 lodash 在 node_modules/lodash 文件夹中没有定义文件,因此它不起作用。您必须使用类型下载它或使用环境声明而不是导入:

declare var _: any;

对于 node.js,您必须使用:

var _ = require('lodash');
于 2016-03-04T20:15:00.873 回答