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
});
})
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
});
})
更多回答
我是一名优秀的程序员,十分优秀!