Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others


0 votes
in Technique[技术] by (71.8m points)

javascript - Why can't you read package.json in a builded electron app?

Hello Stackoverflow users!

I am trying to place all of the configuration for the electron app in the package.json file.

Here is my code snippet:

const { app, BrowserWindow, ipcMain } = require('electron');
const fs = require('fs');

function readConf() {
  const data = fs.readFileSync('./package.json', 'utf8');
  return data;

ipcMain.on('synchronous-message', (event, arg) => {
  event.returnValue = readConfig();


<script type="text/javascript">
const { ipcRenderer } = require('electron')
config = JSON.parse(ipcRenderer.sendSync('synchronous-message', ''))
<h1>Version of <script>document.write(config.name)</script> is <script>document.write(config.version);</script></h1>

The code is working when you run the app via npm start, but when you make it into an exe with electron-forge and squirrel (npm make) and try to run it after installing it throws an error that the package.json file can't be found. So what do you need to specify as the path to the file to read it in the built app?

I am using electron forge.

Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I found a way to fix the issue!

Instead of

const data = fs.readFileSync('./package.json', 'utf8');

You'll need to write

const data = fs.readFileSync(__dirname + '/../package.json', 'utf8');

Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share