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

Importing electron module in renderer(angular) project throws fs,path and webpack polyfill error #632

Closed
nikhilnxvverma1 opened this issue Aug 13, 2021 · 2 comments

Comments

@nikhilnxvverma1
Copy link

Describe the bug
Using (allowed)electron packages in renderer project throws compilation errors

To Reproduce
If I import something in my angular project like:
import { shell } from 'electron';
or
import { nativeImage } from 'electron';
it throws:

./node_modules/electron/index.js:1:9-22 - Error: Module not found: Error: Can't resolve 'fs' in '/Users/nikhilverma/Downloads/stringish-migrated/node_modules/electron'

./node_modules/electron/index.js:3:11-26 - Error: Module not found: Error: Can't resolve 'path' in '/Users/nikhilverma/Downloads/stringish-migrated/node_modules/electron'

BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
This is no longer the case. Verify if you need this module and configure a polyfill for it.

If you want to include a polyfill, you need to:
- add a fallback 'resolve.fallback: { "path": require.resolve("path-browserify") }'
- install 'path-browserify'
If you don't want to include a polyfill, you can use an empty module like this:
resolve.fallback: { "path": false }

Steps to reproduce the behavior:
Hopefully this should be easy:

  1. Just download the current version of this template
  2. In detail.component.ts, at the very top write: import { nativeImage } from 'electron';
  3. In the same file, inside ngOnInit() write: const n = nativeImage.createEmpty();

Expected behavior
A clean compilation build in the console

Screenshots
If applicable, add screenshots to help explain your problem.

Additional context
MacOS 11.4

Add any other context about the problem here.
I am trying to migrate to the latest version of this electron(13.1.7). So far I have successfully converted my entire codebase to angular. But somehow making it work with electron 13 is a huge configuration challenge. Also I am moving from the old one package.json file configuration which had everything under devDependencies.

But the above bug is easily reproducible outside my project environment in the default template project itself.
Thanks.

@nikhilnxvverma1
Copy link
Author

I took cues from electron.service.ts and changed my import declarations

Instead of saying:
import { nativeImage } from 'electron';

I use this syntax:
import { nativeImage as nativeImageType } from 'electron';
const nativeImage:typeof nativeImageType = window.require('electron').nativeImage;

My old codebase worked pleasantly with the first syntax but now I am coerced to use this one instead.Also it took me three days to figure out this bug and these kinds of traps are very annoying during migration.

Nevertheless I am closing this issue.

@nikhilnxvverma1
Copy link
Author

Also since electron v12 contextIsolation needs to be set to false:

StackOverflow

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