0

I have facing against an issue provoked by an npm package which I previouly installed to get the hostname and ip address in my angular project, everything is ok in dev mode but when I build it to get the production files it doesn't work, I have debugged my project and I noticed that everything fails when I use the method to get the hostname. Nowadays what I'm doing is printing in console the next code.

I declared the next variable.

declare var require: any;

And I'm using the next code in my ngOnInit()

const os = require('os');
console.log(os.hostname());

In dev mode it works, but in production mode is fails throwinf the next error.

enter image description here

I my package.json I have the package os in my dependencies.

"dependencies": {
   "os": "^0.1.1",
   ...
}

Does anyone know why this happen?

  • Angular is front-end library and `os` seems to be a NodeJS library. Are you sure you're using the right one / using it correctly ? –  Sep 26 '19 at 14:54
  • Also, `require` is not advised in Typescript, use `import from` syntax instead. –  Sep 26 '19 at 14:55
  • **Maryannah** thaks for your answer, currently I'm using that package in my front-end, so Am I wrong if I use it to get client hostname in my front? or Is there another way to get hostname (pc name) using Angular or js?. Regarding your advice I already tried using `import instead require` Thanks in advance. – Roman Badillo G Sep 26 '19 at 15:04
  • Then yes, you misuse it. You can't get the host name in a front ent project. It's only a backend feature. –  Sep 26 '19 at 15:55
  • 1
    You can't. That's a security breach. The browser doesn't have access to it. –  Sep 26 '19 at 16:47

0 Answers0