通过打字稿设置节点库位置
问题描述:
为了学习打字稿和新的@Types格式,我从blogs from microsoft开始。在博客中,例如使用:通过打字稿设置节点库位置
npm install --save @types/lodash
然后,您只需一个打字稿文件,
import * as _ from "lodash";
_.padStart("Hello TypeScript!", 20, " ");
你然后编译并成为JavaScript的是
"use strict";
exports.__esModule = true;
var _ = require("lodash");
_.padStart("Hello TypeScript!", 20, " ");
然而,我发现这个简单的例子无法执行错误,因为node.js找不到require(“lodash”);库并引发错误。
什么是正确的方式来配置打字稿,以便它正确地引用编译的JavaScript库?
答
这里是如何做到这一点的打字稿的
$ npm install --save lodash
# This is the new bit here:
$ npm install --save @types/lodash
也为@types
可以使用--save-dev
只能在devDependency
安装它,然后,在你的.ts文件:
或者:
import * as _ from "lodash";
Or:
import _ from "lodash";
+0
,一旦我看到它,就会变得非常有意义。 –
你安装了'lodash'吗?我想你还需要'lodash'库。 – Dkouk