gpt4 book ai didi

Can nodejs read project file in Electron application?()

转载 作者:bug小助手 更新时间:2023-10-26 20:54:20 28 4
gpt4 key购买 nike



Can nodejs read project file in Electron project?

NodeJS可以读取Electron项目中的项目文件吗?


I want to use nodejs read source code in my Electron project,

我想在我的Electron项目中使用nodejs读取源代码,


// electron-src/index.ts
import fs from 'fs'
...

ipcMain.on('searchSourcecode', (event: IpcMainEvent, message: any) => {
console.log(message)

fs.readFile("./index.ts", "utf8",function(err, data){
console.log(err);
console.log(data);

event.returnValue = data

});


})

enter image description here


this get error:

此GET错误:


[Error: ENOENT: no such file or directory, open './index.ts'] {
errno: -2,
code: 'ENOENT',
syscall: 'open',
path: './index.ts'
}



and before try to implement this function in Electron, I have tried to read source code in nodejs, it success:

并且在尝试在Electron中实现这一功能之前,我已经尝试在NodeJS中阅读源代码,它成功了:


var fs = require('fs')

const search = () => {
fs.readFile("./myComp01/index.tsx", "utf8", function(err, data){
console.log(data);
});
}

search()

so, why in my Electron project it cannot do it?

那么,为什么在我的电子项目中它做不到呢?


更多回答

Most-likely the path is wrong

最有可能的是这条路是错的

优秀答案推荐

You could use nodejs module path to get the absolute path:

您可以使用NodeJS模块路径来获取绝对路径:


import fs from 'fs'
const path = require('path');
...

ipcMain.on('searchSourcecode', (event: IpcMainEvent, message: any) => {
console.log(message)

const currentDir = __dirname;
const filePath = path.join(currentDir, './index.ts');


fs.readFile(filePath, "utf8",function(err, data){
console.log(err);
console.log(data);

event.returnValue = data
});
})

更多回答

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com