【发布时间】:2021-09-27 01:59:04
【问题描述】:
我正在开发一个带角度的 Electron 应用程序,我想获取一些关于我的电脑的信息并执行一些命令。
我正在尝试使用 os 和 child_process 模块,但到目前为止我无法导入它们。
这是我的组件:
import { Component, OnInit } from '@angular/core';
import * as exec from 'child_process';
@Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.css']
})
export class HomeComponent implements OnInit {
constructor() { }
ngOnInit(): void {
console.log()
}
}
我尝试了许多不同的导入方式,但总是遇到同样的错误:
Error: src/app/components/home/home.component.ts:2:24 - error TS2307: Cannot find module 'child_process' or its corresponding type declarations.
我已经安装了类型:
npm install --save @types/node
这是我的 package.json:
{
"name": "configurator-web",
"version": "0.0.0",
"main": "main.js",
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e",
"start:electron": "ng build --base-href ./ && electron ."
},
"private": true,
"dependencies": {
"@angular/animations": "~11.2.10",
"@angular/common": "~11.2.10",
"@angular/compiler": "~11.2.10",
"@angular/core": "~11.2.10",
"@angular/forms": "~11.2.10",
"@angular/platform-browser": "~11.2.10",
"@angular/platform-browser-dynamic": "~11.2.10",
"@angular/router": "~11.2.10",
"@fortawesome/fontawesome-free": "^5.15.3",
"chai": "^4.3.4",
"rxjs": "~6.6.0",
"tslib": "^2.0.0",
"zone.js": "~0.11.3"
},
"devDependencies": {
"@angular-devkit/build-angular": "~0.1102.9",
"@angular/cli": "~11.2.9",
"@angular/compiler-cli": "~11.2.10",
"@types/jasmine": "~3.6.0",
"@types/node": "^12.11.1",
"codelyzer": "^6.0.0",
"electron": "^12.0.7",
"jasmine-core": "~3.6.0",
"jasmine-spec-reporter": "~5.0.0",
"karma": "~6.1.0",
"karma-chrome-launcher": "~3.1.0",
"karma-coverage": "~2.0.3",
"karma-jasmine": "~4.0.0",
"karma-jasmine-html-reporter": "^1.5.0",
"mocha": "^8.4.0",
"protractor": "~7.0.0",
"spectron": "^14.0.0",
"ts-node": "~8.3.0",
"tslint": "~6.1.0",
"typescript": "~4.1.5"
}
}
到目前为止,我尝试使用 os 和 child_process 并得到完全相同的错误。我检查了,文件夹和 .ts 文件在安装时存在于 node_modules 下。
任何帮助将不胜感激。我不知道还能做什么。
谢谢!
【问题讨论】:
-
浏览器模式下无法访问节点模块...
-
它是服务器(node.js)上的一个模块,而不是客户端(用户的浏览器)上的一个模块。您不能从用户的浏览器生成进程。在您的情况下,Electron 是浏览器 和 服务器,但它在内部也是分开的。
-
查看electronjs.org/docs/tutorial/…了解如何操作
-
你能分享一下tsconfig.json文件的代码吗?
标签: node.js angular npm electron