我的项目是使用angular cli [版本-6.1.3]创建的。 我安装了npm模块-is-reachable,并在我的代码中将其用作-
const isReachable = require('is-reachable');
appDetailsFromJson.forEach(app => {
isReachable(app.server).then(reachable => {
console.log('Hey --> ',reachable);
//=> true
});
但是,在运行项目时会引发异常-错误TS2304:找不到名称“ require”。
此问题的根本原因是什么?以角度6导入库的正确方法是什么?
答案 0 :(得分:2)
在NPM page of isReachable中,(我强调):
在Node.js和浏览器( with browserify )中工作。
这意味着它不太可能在Angular应用程序中正常工作,因为Angular CLI使用webpack和标准的Typescript编译器(而不是browserify)来解决导入和包依赖关系。
通常,Angular中的导入为standard ES6-style 'import' statements,例如:
import { isReachable } from 'is-reachable';
...或...
import * as isReachable from 'is-reachable';
如果is-reachable本身不使用任何其他require()语句,则此可以起作用,但是如果它在自己的代码中使用require来引入其依赖项,则可能会使其完全无法正常工作-寻找满足您要求的其他方法几乎肯定会更好。