question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Paths for native modules and binaries imported by a renderer process being changed to .webpack/main instead of .webpack/renderer

See original GitHub issue

Preflight Checklist

  • I have read the contribution documentation for this project.
  • I agree to follow the code of conduct that this project follows, as appropriate.
  • I have searched the issue tracker for a bug that matches the one I want to file, without success.

Issue Details

  • Electron Forge Version:
    • 6.0.0-beta.50
  • Electron Version:
    • 8.1.1
  • Operating System:
    • Windows 10 (1903)
  • Last Known Working Electron Forge version::
    • N/A

Expected Behavior

When using trash (dependency for deleting files and moving them to the current OS’ native recycling bin), the executables macos-trash and windows-trash.exe should be copied over to .webpack/renderer/native_modules and the path to these executables in the main_window/index.js file should reflect the renderer path.

Actual Behavior

macos-trash and windows-trash.exe are properly copied over to .webpack/renderer/native_modules but the path in main_window/index.js was rewritten to .webpack/main instead of .webpack/renderer. When trying to delete a file, it throws a an error:

Uncaught (in promise) Error: spawn C:\path\to\testElectronTrashApp\.webpack\main/../native_modules/windows-trash.exe ENOENT
    at Process.ChildProcess._handle.onexit (internal/child_process.js:264)
    at onErrorNT (internal/child_process.js:456)
    at processTicksAndRejections (internal/process/task_queues.js:80)

To Reproduce

  1. Create a new project and install trash and string-replace-loader.
npx create-electron-app testElectronTrashApp --template=webpack
cd testElectronTrashApp
npm install trash
  1. Create an empty text file named temp.txt
  2. In webpack.rules.js add the following lines (workaround for default export):
{
    test: /parsers\.js$/,
    loader: 'string-replace-loader',
    options: {
      search: 'module.exports = parsers;',
      replace: 'export default parsers;',
    }
  }
  1. In the main renderer file import trash and try to delete temp.txt:
import trash from 'trash';
trash('C:\\path\\to\\testElectronTrashApp\\temp.txt')
  1. Run the app: npm start

Additional Information

It seems like the fix is fairly straightforward. In the Webpack.ts file under the Webpack plugin, on line 100 inRendererDir should be passed to the assetRelocatorBaseDir function.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:7
  • Comments:9

github_iconTop GitHub Comments

1reaction
ysdexliccommented, Jun 28, 2020

hey @mohyddintash, I fixed by following the step left in the comment and a bit more:

  • As said by @Mac-Genius, “In the Webpack.ts file under the Webpack plugin, on line 100 inRendererDir should be passed to the assetRelocatorBaseDir function.”, you can find this file in the node_modules although it’s not the safest thing to do to fix the issue (it’ll be overwritten every time you install deps)
  • After that fix, a new bug arose about a directory not existing. If you come across this the fix is - in the same file at line 86 and line 93 they reference any_folder. As the any_folder directory didn’t exist this was erroring. I changed it to be the name of my main window main_window as a hack (any directory in the renderer directory should work) but the real fix would be to stop whatever is moving up a directory (adding ../ to the path) as they’ve clearly added any_folder in an attempt to negate it.

I’m not working on the project anymore but after resolving these two issues I ran into even more but I think they were quite library specific.

I hope this helps, let me know if you have any more questions.

0reactions
Chris533commented, Nov 4, 2020

Hi @Mac-Genius @ysdexlic @MarshallOfSound I’m also runnig into the same issue, how can I fix it or go around it untill it’s fixed? Thanks!

Using native module in renderer, you can copy from node_modules manually, not perffect, duplicate with renderer/native_modules (e.g. better-sqlite3):

image

yarn add copy-webpack-plugin -D

webpack.plugins.js

const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
const CopyPlugin = require('copy-webpack-plugin');

module.exports = [
  new CopyPlugin({
    patterns: [
      { from: './node_modules/better-sqlite3/build/Release/better_sqlite3.node', to: '../native_modules/build/Release/better_sqlite3.node' },
    ],
  }),
  new ForkTsCheckerWebpackPlugin(),
];

Tested in start and package.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Issues - GitHub
Paths for native modules and binaries imported by a renderer process being changed to .webpack/main instead of .webpack/renderer #1559.
Read more >
Using Node.js addons in Electron's renderer with Webpack
Problem. The first problem is that node-bindings , which node-serialport relies on to resolve the path to its Node.js addon, simply doesn't ...
Read more >
ioHook + Electron + Webpack integration | by Ievgenii Spitsyn
ioHook is a Node.js global native keyboard and mouse listener. ... Running ioHook from Electron main vs Electon renderer process, app.
Read more >
Prerequisites for Libraries - React Native
The following steps will help ensure your modules and components are ready for the New Architecture.
Read more >
The Future of Native Modules in Node.js - NearForm
One that is exciting us is the update to the native modules library n-api. It comes out of experimental status in the upcoming...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found