【问题标题】:Failed to compile : ./node_modules/macaddress/lib/windows.js in Angular 5编译失败:Angular 5 中的 ./node_modules/macaddress/lib/windows.js
【发布时间】:2019-02-13 22:39:43
【问题描述】:

我是 Angular 5 的新手,根据我的业务需要,我需要获取用户机器的 ma​​c 地址。为此,我尝试实现以下...

参考:https://www.npmjs.com/package/macaddress

npm install --save macaddress

我已经使用 NPM 添加了这个包,并在我的 typescript 文件中添加了以下代码。

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);  
    });
}

添加此代码后,我无法运行我的项目,并显示带有上述错误的黑屏。

谁能帮我解决这个问题。

编辑:./node_modules/macaddress/lib/windows.js 找不到模块:错误:无法解析“D:\Projects\Angular\eCommerceWebsite\node_modules\macaddress\lib”中的“child_process” 错误@vendor.js:158535

在控制台中得到了这个。

更新:

【问题讨论】:

  • 您能检查一下浏览器控制台上显示的错误吗?
  • 请查看更新后的帖子@SandeepKamath
  • 您是否在类型脚本文件中导入了您正在使用的新模块。你能添加你的打字稿文件的代码sn-p吗?
  • 我在我的 app.component.ts 文件中使用此代码。@SandeepKamath
  • 好的。基本上你需要在 app modules 类中添加一个新模块,然后在组件 ts 文件中导入。请在下面的代码 sn-p 中找到一个示例

标签: angular typescript angular5


【解决方案1】:

你不能在 Angular 应用中使用这个库。出于安全原因,您根本无法从浏览器中查询用户的 mac 地址。该库适用于服务器端 node.js 应用程序,与任何客户端应用程序无关。 More info here

【讨论】:

  • 那么有没有办法通过系统的任何唯一身份来识别每个用户。请指导我@VahidN
【解决方案2】:

这是在应用模块文件 app.module.ts 中添加模块的示例,例如“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 { }

【讨论】:

  • 我认为那是因为它只是 npm 代码。但是在你执行 npm install 之后,你能在你的 ng_modules 文件夹中看到 macaddress 模块吗?
  • 是的,我有。我真的很困惑所有文件都在那里但无法正常工作,请参阅更新后的帖子@Sandeepkamath
  • 是的,如果你使用 Visual Studio Code。如果您尚未在 app.module.ts 文件中添加模块,您将获得添加模块的选项。正如我在示例中给出的那样,例如'BrowerModule'
  • 那么我应该怎么做才能让它工作,请给一个解决方案兄弟我真的很挣扎这个@SandeepKamath
  • 1.首先,我认为您需要在 app.module.ts 文件中添加与 BrowserModule 相同的模块。 2.然后在你的app.component.ts中导入模块,如果你在上面两点这样做它应该可以工作。
猜你喜欢
  • 2020-09-03
  • 1970-01-01
  • 2018-10-27
  • 1970-01-01
  • 2017-03-10
  • 1970-01-01
  • 1970-01-01
  • 2020-05-27
  • 2023-03-13
相关资源
最近更新 更多