【发布时间】:2021-09-26 05:49:41
【问题描述】:
我对堆栈溢出还很陌生,所以如果我没有正确提出问题,请随时帮助我。
我搜索了所有我能找到的堆栈溢出和谷歌文章,但导入 Electron、任何其他 Node 模块或任何本机 JS 模块都不起作用——我只能导入和使用 Angular/typescript 模块。我正在尝试导入电子并在有角度的应用程序中使用它。我也在尝试使用 __dirname。对于电子我试过:
const { remote } = require('electron');
const { remote } = (<any>window)require('electron');
import { ipcRenderer, BrowserWindow, electron } from 'electron';
import * as remote from '@electron/remote'
对于 __dirname 我试过了:
import * as fs from 'fs';
import { readFileSync } from 'fs';
import readFileSync from 'fs';
对于实施:
import.meta.url
process.cwd()
__dirname //worth a shot I guess
我已经组合了这些选项,但没有任何效果。我运行了npm install --save-dev @types/node,当它不起作用时尝试删除node_modules 文件夹并运行npm install。 "types": ["node"] 已经添加到 tsconfig.json 的 compilerOptions 中。
这是我的错误之一:
Error: src/app/electron/electron.service.ts:3:20 - error TS2591: Cannot find name 'require'. Do you need to install type definitions for node? Try `npm i --save-dev @types/node` and then add `node` to the types field in your tsconfig.
3 const { remote } = require('electron');
~~~~~~~
我已经安装了@types/node。它也几乎总是发布以下错误。我不知道它的用途,因为即使我没有导入“fs”,它也会显示出来。
Error: ./node_modules/electron/index.js
Module not found: Error: Can't resolve 'fs' in '/Users/user/Programming/Git/project/node_modules/electron'
resolve 'fs' in '/Users/user/Programming/Git/project/node_modules/electron'
Parsed request is a module
using description file: /Users/user/Programming/Git/project/node_modules/electron/package.json (relative path: .)
Field 'browser' doesn't contain a valid alias configuration
resolve as module
looking for modules in /Users/user/Programming/Git/project
using description file: /Users/user/Programming/Git/project/package.json (relative path: .)
【问题讨论】:
标签: javascript node.js angular typescript electron