Unable to load preload script on windows 7 environment


#1

I am trying to launch simple browserwindow using below code in main.js. preload.js script is in the same directory as main.js

mainWindow = new BrowserWindow({
  'width': width,
  'height': height,
  'max-width': width,
  'max-height': height,
  'fullscreen': false,
  'frame': false,
  'kiosk': false,
  'transparent': true,
  'show': true,
  'resizable': false,
  'node-integration': false,
  'preload':__dirname + '\\preload.js'
});

preload script loads fine on OS X machine but shows below error on windows 7 machine.

C:\Temp\electron\electron-quick-start\node_modules\electron-prebuilt\dist\resources\electron.asar\rendrer\init.js:129 Unable to load preload script C:\Temp\electron\electron-quick-start\preload.js

I have confirmed that perload.js script exists in C:\Temp\electron\electron-quick-start directory.

Wondering whats going on here?


#2

for anyone facing this issue, we need to prefix ‘file://’ for preload script path. It works in OS X without it. Here is new code snippet that works for windows platform

mainWindow = new BrowserWindow({
‘width’: width,
‘height’: height,
‘max-width’: width,
‘max-height’: height,
‘fullscreen’: false,
‘frame’: false,
‘kiosk’: false,
‘transparent’: true,
‘show’: true,
‘resizable’: false,
‘node-integration’: false,
‘preload’:‘file://’+__dirname + ‘\preload.js’
});


#3

Probably needed the file: because of the drive letter c: … which would look like a protocol specifier otherwise. Might have worked with just a relative or absolute path without a drive letter.

good to know though.