angular - How can I use node/npm-modules in angular project?
问题描述
I want to use node and npm modules in my angular project (angular-electron-app).
For example I want to use the module 'dns' (node module) in angular to make a dns lookup after button click event.
This is the code how to use it with node.
const dns = require('dns');
dns.lookup('google.com', options, (err, addresses) =>{
if(err != null)
{
console('DNS Error');
console.log(err);
}
else
{
console.log('IP-Adresse: ');
console.log(addresses);
}
});
Now I want to use this functionality directly in my component.ts file in angular and my browser/electron app. That means after button click event the dns lookup should fired.
My problem now is how to import node modules or generally modules from extern sources (npm modules) in my project.
I tried it with this code in my component.ts:
import * as lookDNS from 'dns-lookup';
dnsCheck(): void {
console.log('dns check');
lookDNS.lookup('www.google.de', function (err, address, family) {
console.dir(err);
});
}
but the problem is that the browser cannot run javascript with 'require'.
Thanks for help!
解决方案
推荐阅读
- python - 使用 Python 字典确定要删除哪些重复图像
- python - python discord.py 中的 JSON
- node.js - 安装@ngrx/store 时如何清除错误?
- json - 在 Groovy(或 JOLT)中修改 JSON
- php - 当试图在 php 中将此数组导出为 CSV 文件时,我得到一张空表
- java - 如何在 SpringBoot 中以线程安全的方式执行 @Scheduled 作业?
- r - 使用相同任务的 svm 和 ranger 的不同运行时
- python - 使用statsmodels的“plot_acf”绘制自相关函数时如何获取阴影区域的坐标
- c++ - 使用变量的一部分作为布尔值
- python - TP、TN、FP 和 FN 的总数不等于观测值的总数