Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot import module 'os' in Electron

I am doing an easy test to try to get my computer's name in an Electron app using Angular and nodejs.

This is my script:

import { Component, OnInit } from '@angular/core';
import { hostname } from 'os'

@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.css']
})
export class HomeComponent implements OnInit {

  constructor() { }

  ngOnInit(): void {
    console.log(hostname);
  }

}

I also installed npm i @types/node

I get the following error:

Error: src/app/components/home/home.component.ts:2:26 - error TS2307: Cannot find module 'os' or its corresponding type declarations. import { hostname } from 'os'

Any ideas? this should be straightforward and I feel so silly with such a problem...

Edit: I added that this is an electron app!!

like image 764
Carloshf Avatar asked Nov 24 '25 10:11

Carloshf


1 Answers

Before using import * as os from 'os';, you have to make sure "types":["node"] in the tsconfig.app.json

{
  "compilerOptions": {
    "types": [
      "node"
    ]
  }
}
like image 149
rockcode Avatar answered Nov 26 '25 00:11

rockcode



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!