user5156141
user5156141

Reputation: 684

Electron Forge v6, Keytar & Node-Loader Error "no suitable image found... file too short"

I am having some trouble getting the Keytar package working in an Electron Forge v6 project (beta.54).

In a freshly generated project, I receive an error about "file too short" when I run the application. Below is an excerpt of the error dialog shown originating from the main process:

> electron-forge start

✔ Checking your system
✔ Locating Application
✔ Preparing native dependencies: 1 / 1
✔ Compiling Main Process Code
✔ Launch Dev Servers
✔ Compiling Preload Scripts
✔ Launching Application


Webpack Output Available: http://localhost:9000

App threw an error during load
Error: node-loader:
Error: dlopen(/Users/abc/Desktop/app2/.webpack/main/9e6f9bf6669542cb15efba32c033f3de.node, 1): no suitable image found.  Did find:
    /Users/abc/Desktop/app2/.webpack/main/9e6f9bf6669542cb15efba32c033f3de.node: file too short
    /Users/abc/Desktop/app2/.webpack/main/9e6f9bf6669542cb15efba32c033f3de.node: file too short
    at Object.<anonymous> (/Users/abc/Desktop/app2/.webpack/main/index.js:1015:9)
    at Object../node_modules/keytar/build/Release/keytar.node (/Users/abc/Desktop/app2/.webpack/main/index.js:1018:30)
    at __webpack_require__ (/Users/abc/Desktop/app2/.webpack/main/index.js:21:30)
    at Object../node_modules/keytar/lib/keytar.js (/Users/abc/Desktop/app2/.webpack/main/index.js:1029:14)
    at __webpack_require__ (/Users/abc/Desktop/app2/.webpack/main/index.js:21:30)
    at Object../src/index.ts (/Users/abc/Desktop/app2/.webpack/main/index.js:1440:14)
    at __webpack_require__ (/Users/abc/Desktop/app2/.webpack/main/index.js:21:30)
    at /Users/abc/Desktop/app2/.webpack/main/index.js:85:18
    at Object.<anonymous> (/Users/abc/Desktop/app2/.webpack/main/index.js:88:10)
    at Module._compile (internal/modules/cjs/loader.js:1152:30)

It seems that node-loader is having trouble with this package. I cannot decipher what is happening as I can see the package .node files are copied into ./webpack/main/native_modules , but there is a hashed filename at the root which seems to import the native file.

One strange thing I did notice is that the hashed file at .webpack/main/9e6f9bf6669542cb15efba32c033f3de.node contains the following and not the native binary data:

module.exports = __non_webpack_require__("./native_modules/build/Release/keytar.node")

Subsequently... these .node files /native_modules/build/Release/keytar.node are what I expect.

How should one proceed? I didn't rush to open issues on GitHub until I figured where the issue was coming from.

I feel at the root, there's an issue with how the .node file is generated in keytar, but then who even knows what black magic is happening with Webpack and the loaders.

Steps to reproduce

  1. Create a new Electron Forge project `npx create-electron-app app2 --template=typescript-webpack
  2. Install Keytar npm install keytar
  3. Add const key tar = require('keytar'); into src/index.ts
  4. Run the app via npm run start
  5. Experience the error

Versions

Node: v14.14.0
NPM: v7.0.13

Package JSON dependencies:

"devDependencies": {
    "@electron-forge/cli": "^6.0.0-beta.54",
    "@electron-forge/maker-deb": "^6.0.0-beta.54",
    "@electron-forge/maker-rpm": "^6.0.0-beta.54",
    "@electron-forge/maker-squirrel": "^6.0.0-beta.54",
    "@electron-forge/maker-zip": "^6.0.0-beta.54",
    "@electron-forge/plugin-webpack": "6.0.0-beta.54",
    "@marshallofsound/webpack-asset-relocator-loader": "^0.5.0",
    "@typescript-eslint/eslint-plugin": "^4.0.1",
    "@typescript-eslint/parser": "^4.0.1",
    "css-loader": "^4.2.1",
    "electron": "11.0.3",
    "eslint": "^7.6.0",
    "eslint-plugin-import": "^2.20.0",
    "fork-ts-checker-webpack-plugin": "^5.0.14",
    "node-loader": "^1.0.1",
    "style-loader": "^1.2.1",
    "ts-loader": "^8.0.2",
    "typescript": "^4.0.2"
  },
  "dependencies": {
    "electron-squirrel-startup": "^1.0.0",
    "keytar": "^7.2.0"
  }

Upvotes: 3

Views: 1630

Answers (1)

epokal
epokal

Reputation: 91

I had pretty much exact replica of this issue with native module loading the only difference being that in my case the module was locally built and installed as a part of the project.

Something in the webpack's support for native modules has issues within this configuration and causes this. What did the trick for me was to remove node-loader in webpack in webpack.rules.js, ie removing this section:

-  // Add support for native node modules
-  {
-    test: /\.node$/,
-    use: 'node-loader',
-  },

I found this solution and further discussion on the topic from the issue tracker of electron-forge: https://github.com/electron-userland/electron-forge/issues/1688

Upvotes: 5

Related Questions