Paths for native modules and binaries imported by a renderer process being changed to .webpack/main instead of .webpack/renderer
See original GitHub issuePreflight 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
- Create a new project and install
trash
andstring-replace-loader
.
npx create-electron-app testElectronTrashApp --template=webpack
cd testElectronTrashApp
npm install trash
- Create an empty text file named
temp.txt
- 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;',
}
}
- In the main renderer file import trash and try to delete
temp.txt
:
import trash from 'trash';
trash('C:\\path\\to\\testElectronTrashApp\\temp.txt')
- 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:
- Created 4 years ago
- Reactions:7
- Comments:9
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
hey @mohyddintash, I fixed by following the step left in the comment and a bit more:
node_modules
although it’s not the safest thing to do to fix the issue (it’ll be overwritten every time you install deps)any_folder
. As theany_folder
directory didn’t exist this was erroring. I changed it to be the name of my main windowmain_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 addedany_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.
Using native module in renderer, you can copy from node_modules manually, not perffect, duplicate with renderer/native_modules (e.g. better-sqlite3):
yarn add copy-webpack-plugin -D
webpack.plugins.js
Tested in start and package.