【发布时间】:2016-11-08 12:26:31
【问题描述】:
我正在试一试打字稿。它在 hello world 阶段运行良好。我现在正在尝试使用 npm 模块:
index.ts =
import _ = require('lodash')
console.log(_.toUpper('Hello, world !'))
这不起作用:
-
tsc index.ts->Cannot find module 'lodash'. (2307) -
node-ts index.js->Cannot find module 'lodash'. (2307)
查看打字稿文档和在谷歌中没有帮助。其他 S/O 问题要么没有答案(here 和 here),要么不相关。
元素:
- typescript 1.8 最新版
- 是的,lodash 已安装
npm i --save lodash并存在于我的文件系统中(已选中) - 我也做过
typings i --save lodash - 变体
import * as _ from 'lodash'或const _ = require('lodash')也不起作用 - 我尝试按照其他答案
"moduleResolution": "node"和"module": "commonjs"中的建议调整 tsconfig.json 选项,如某些答案中的建议,仍然不起作用
我们如何在 typescript 中使用 npm 包??
【问题讨论】:
-
您是否在 index.ts 中添加了对 lodash.d.ts 的引用?它应该类似于:
///<reference path="../typings/lodash/lodash.d.ts"/> -
@Granga 它有效。您可以将此添加为答案吗?
-
很高兴它有效。 Blackus 已经添加了答案,它指定了我更好的建议。不过需要注意的是:当在命令行上指定输入文件时(这是您的情况),tsconfig.json 文件将被忽略。 (source)
标签: node.js typescript npm tsc