0

I am new to Angular 5 ,as per my business need I need to get the user machine's mac address.For that I tried to implement the following...

enter image description here

Reference: https://www.npmjs.com/package/macaddress

npm install --save macaddress

I have added this package using NPM and the following code in my typescript file .

var macaddress = require('macaddress'); //added this code below my imports

 ngOnInit() {
    macaddress.one(function (err, mac) {
      console.log("Mac address for this host: %s", mac);  
    });
}

After adding this codes I can't run my project and it showing a black screen with the above error .

Can anyone help me to fix this please.

Edit : ./node_modules/macaddress/lib/windows.js Module not found: Error: Can't resolve 'child_process' in 'D:\Projects\Angular\eCommerceWebsite\node_modules\macaddress\lib' errors @ vendor.js:158535

got this in console.

Update :

enter image description here

Zhu
  • 3,679
  • 14
  • 45
  • 76

2 Answers2

3

You can't use this library from an Angular app. You can't query the user's mac address from a browser at all for security reasons. That library is intended for server-side node.js apps and has nothing to do with any client-side app's. More info here

VahidN
  • 18,457
  • 8
  • 73
  • 117
  • So is there any way to identify each user by any unique identity of the system .please guide me @VahidN – Zhu Sep 10 '18 at 13:55
-1

This is an example of adding a module in app module file app.module.ts for example 'BrowserModule'

import { NgModule }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent }  from './app.component';

@NgModule({
 imports:      [ BrowserModule ],
 declarations: [ AppComponent ],
 bootstrap:    [ AppComponent ]
})
export class AppModule { }
Sandeep Kamath
  • 670
  • 9
  • 15
  • I think that is because it just npm code. But after you do npm install , are you able to see macaddress module inside your ng_modules folder ? – Sandeep Kamath Sep 09 '18 at 07:48
  • yes I have it . I am really confused all the files are there but not working please see the updated post@Sandeepkamath – Zhu Sep 09 '18 at 09:21
  • yes then if you use Visual Studio Code. you will get options of adding the module, if you have not added the module in app.module.ts file. as given in the example by me for example 'BrowerModule' – Sandeep Kamath Sep 09 '18 at 14:18
  • So what should I do to make it work ,please give a solution brother I am really struggling with this @SandeepKamath – Zhu Sep 09 '18 at 14:19
  • 1. first i think you need to add the module in app.module.ts file same as that of BrowserModule. 2. Then import the module in your app.component.ts if you do this above two point it should work. – Sandeep Kamath Sep 09 '18 at 14:21
  • I can’t find any module name I tried to type getmac and Macaddress but there I cannot find anything like that ....@SandeepKamath – Zhu Sep 09 '18 at 14:22