Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to Load Preload Script in Electron: The URL must be of scheme file #76

Open
iosimliviu opened this issue Jun 21, 2023 · 0 comments

Comments

@iosimliviu
Copy link

Requirement:

  • update npm package from version handbrake-js@^5.0.3 to handbrake-js@^7.0.0 in an Electron application

Error encountered:

node:electron/js2c/renderer_init:73 Unable to load preload script: /Users/user/Documents/handbrake_update/electron/app/preload.js
node:electron/js2c/renderer_init:73 TypeError: The URL must be of scheme file
    at __node_internal_captureLargerStackTrace (node:internal/errors:465:5)
    at new NodeError (node:internal/errors:372:5)
    at Object.fileURLToPath (node:internal/url:1424:11)
    at getModulePaths (/Users/user/Do…ist/index.cjs:12:26)
    at Object.<anonymous> (/Users/user/Do…ist/index.cjs:17:36)
    at Object.<anonymous> (/Users/user/Do…ist/index.cjs:676:3)
    at Module._compile (node:internal/modules/cjs/loader:1120:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1175:10)
    at Module.load (node:internal/modules/cjs/loader:988:32)
    at Module._load (node:internal/modules/cjs/loader:829:12)
    at c._load (node:electron/js2c/asar_bundle:5:13343)
    at i._load (node:electron/js2c/renderer_init:33:356)
    at Module.require (node:internal/modules/cjs/loader:1012:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.<anonymous> (/Users/user/Do…pp/preload.js:90:23)
    at Object.<anonymous> (/Users/user/Do…pp/preload.js:102:3)
    at Module._compile (node:internal/modules/cjs/loader:1120:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1175:10)
    at Module.load (node:internal/modules/cjs/loader:988:32)
    at Module._load (node:internal/modules/cjs/loader:829:12)
    at c._load (node:electron/js2c/asar_bundle:5:13343)
    at i._load (node:electron/js2c/renderer_init:33:356)
    at Object.<anonymous> (node:electron/js2c/renderer_init:73:2296)
    at ./lib/renderer/init.ts (node:electron/js2c/renderer_init:73:2421)
    at __webpack_require__ (node:electron/js2c/renderer_init:1:170)
    at node:electron/js2c/renderer_init:1:1242
    at ___electron_webpack_init__ (node:electron/js2c/renderer_init:1:1310)
    at node:electron/js2c/renderer_init:141:194
    at NativeModule.compileForInternalLoader (node:internal/bootstrap/loaders:312:7)
    at NativeModule.compileForPublicLoader (node:internal/bootstrap/loaders:252:10)
    at loadNativeModule (node:internal/modules/cjs/helpers:49:9)
    at Module._load (node:internal/modules/cjs/loader:811:15)
    at c._load (node:electron/js2c/asar_bundle:5:13343)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
    at node:internal/main/run_main_module:17:47

Problem:

The error is coming from the following file:

`.../electron/app/node_modules/handbrake-js/dist/index.cjs`

At this current block of code:

function getModulePaths (fileURL) {
  const __filename = url.fileURLToPath(fileURL);
  const __dirname = path.dirname(__filename);
  return { __filename, __dirname }
}

const { __dirname: __dirname$1 } = getModulePaths((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (document.currentScript && document.currentScript.src || new URL('index.cjs', document.baseURI).href)));

The url to the HandbrakeCLI binary doesn't seem to resolve as being of type file:// when the package is installed in an electron environment, thus being blocked at the following function (in Object.fileURLToPath (node:internal/url:1424:11)):

function fileURLToPath(path) {
  if (typeof path === 'string')
    path = new URL(path);
  else if (!isURLInstance(path))
    throw new ERR_INVALID_ARG_TYPE('path', ['string', 'URL'], path);
  if (path.protocol === 'electron:')
    return 'electron';
  if (path.protocol !== 'file:')
    throw new ERR_INVALID_URL_SCHEME('file');
  return isWindows ? getPathFromURLWin32(path) : getPathFromURLPosix(path);
}

setup:

  • process.platform = darwin
  • process.arch = arm64
  • node version: v19.1.0
  • "electron": "^21.3.1"

Any tips to get the URL right would be greately appreciated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant