如何在Angular 6中导入Node npm模块

6

我已经安装了该包

npm install local-devices

并将其作为angular.json中的导入项

"scripts": [
    "./node_modules/local-devices/index.js"
]

并在 src/typings.d.ts 中声明为 全局变量

declare let findLocalDevices: any

在下面,是local-devices包的package文件结构和index.js

enter image description hereenter image description here

然后尝试将其用作组件中的代码。
console.log(findLocalDevices())

但是出现了错误

找不到 findLocalDevices 的定义

请指导如何导入它!!!

1个回答

9
import * as findLocalDevices from 'local-devices'

在任何方法内部使用它:

ngOnInit() {
   console.log(findLocalDevices());
}

您可能需要安装其他包,例如os、child_process和mz。


2
模块未找到:错误:无法解析“\node_modules\mz”中的“child_process”。如何解决? - WasiF

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接