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

Can't use npm packages with TS module import from Angular that uses NodeJS core module #657

Closed
laurentperroteau opened this issue Nov 16, 2021 · 2 comments

Comments

@laurentperroteau
Copy link

Describe the bug
I can't use 3rd party libraries with TS module import from Electron's Renderer process (Angular) that uses NodeJS core module.

To Reproduce
Steps to reproduce the behavior:

  1. npm install fs-extra --save
  2. cd app && npm install fs-extra --save && cd ..
  3. Import package in app.component.ts: import * as fsExtra from 'fs-extra'; (same problems with 3 others package that depend of 'fs')
  4. Use it in app.component.ts :
fsExtra.copy('/tmp/myfile', '/tmp/mynewfile')
      .then(() => console.log('success!'))
      .catch(err => console.error(err));
  1. npm start

Errors

./node_modules/graceful-fs/graceful-fs.js:1:9-22 - Error: Module not found: Error: Can't resolve 'fs' in '/home/.../angular-electron/node_modules/graceful-fs'

./node_modules/jsonfile/index.js:10:8-21 - Error: Module not found: Error: Can't resolve 'fs' in '/home/.../angular-electron/node_modules/jsonfile'

Additional context

  • OS: Linux Ubuntu 20.04. LTS

Thanks in advance for the help, I know is not anymore the recomend way to use Node library in the rendered context but my app start with this template 5 years ago in this way

@maximegris
Copy link
Owner

maximegris commented Nov 17, 2021

Hi

Please have a look at README.md

electron.service.ts

[...]
import * as fsExtra from 'fs-extra';

@Injectable({
  providedIn: 'root'
})
export class ElectronService {
  [...]
  fsExtra: typeof fsExtra;

  [...]

  constructor() {
    // Conditional imports
    if (this.isElectron) {
      [...]
      this.fsExtra = window.require('fs-extra');
    }
  }
}

app.component.ts

 if (electronService.isElectron) {
      electronService.fsExtra.copy('/tmp/myfile', '/tmp/mynewfile')
            .then(() => console.log('success!'))
            .catch(err => console.error(err));
}

This perfectly works.

@laurentperroteau
Copy link
Author

Thanks, so if another person is in the same case: it is no longer possible (it was at least until v6.0.1) to import packages which depend on core Node like any browser package.

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

2 participants