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.

How to use native node modules?

import { Component } from '@angular/core';
import * as c from 'child_process';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent {
  title = 'app works!';

  constructor() {
    console.log('c', c);
  }
}

Webpack error:

$ npm run build

> angular-electron@1.0.3 build C:\Users\daniel\projects\angular-electron
> ng build && copyfiles main.js dist

Hash: f817a55010bbe497b5df
Time: 8290ms
chunk    {0} polyfills.bundle.js, polyfills.bundle.js.map (polyfills) 158 kB {4} [initial] [rendered]
chunk    {1} main.bundle.js, main.bundle.js.map (main) 3.98 kB {3} [initial] [rendered]
chunk    {2} styles.bundle.js, styles.bundle.js.map (styles) 9.89 kB {4} [initial] [rendered]
chunk    {3} vendor.bundle.js, vendor.bundle.js.map (vendor) 2.07 MB [initial] [rendered]
chunk    {4} inline.bundle.js, inline.bundle.js.map (inline) 0 bytes [entry] [rendered]

ERROR in C:/Users/daniel/projects/angular-electron/src/app/app.component.ts (2,20): Cannot find module 'child_process'.

ERROR in C:/Users/daniel/projects/angular-electron/src/app/app.component.ts (2,20): Cannot find module 'child_process'.

ERROR in ./src/app/app.component.ts
Module not found: Error: Can't resolve 'child_process' in 'C:\Users\daniel\projects\angular-electron\src\app'
 @ ./src/app/app.component.ts 11:0-35
 @ ./src/app/app.module.ts
 @ ./src/main.ts
 @ multi ./src/main.ts

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
maximegriscommented, Apr 12, 2017

Hi,

By default, angular-cli doesn’t seem to be able to import nodeJS native libs or electron libs. The best solution is to override webpack configuration.

I did a test and it seems to work

import { Component } from '@angular/core';
import { ipcRenderer } from 'electron';
import * as childProcess from 'child_process';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent {
  title = 'app works!';

  constructor() {
    // Check if electron is correctly injected (see externals in webpack.config.js)
    console.log('c', ipcRenderer);
    // Check if nodeJs childProcess is correctly injected (see externals in webpack.config.js)
    console.log('c', childProcess);

  }
}

capture

I pushed the fix on the master branch and add a new tag 1.1.0. You have to do a yarn or npm installation to install the new dependencies.

Tell me if this correction fix your error.

0reactions
shivarajnaiducommented, Oct 21, 2018

Kindly point some links/tutorials to do this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

CommonJS modules | Node.js v19.3.0 Documentation
CommonJS modules are the original way to package JavaScript code for Node.js. Node.js also supports the ECMAScript modules standard used by browsers and ......
Read more >
Node.js Modules - W3Schools
What is a Module in Node.js? Consider modules to be the same as JavaScript libraries. A set of functions you want to include...
Read more >
node-modules - npm
commandline tool and node module for node-modules.com. Latest version: 1.0.1, last published: 7 years ago. Start using node-modules in your ...
Read more >
Node.js Modules - GeeksforGeeks
In Node.js, Modules are the blocks of encapsulated code that communicates with an external application on the basis of their related ...
Read more >
Install Node.js modules - Bitnami Documentation
Using it, you can install, uninstall and search for Node.js modules. npm installs modules in two different scopes: local and global. Local installation....
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