首页 > 解决方案 > 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!

标签: angularimportmodule

解决方案


推荐阅读