Require nodejs “child_process” with TypeScript, SystemJS and Electron

前端 未结 3 401
独厮守ぢ
独厮守ぢ 2020-12-18 23:42

I\'m working on a simple nodejs electron (formerly known as atom shell) project. I\'m writing it using angular 2, using the project the same project setup as they recommend

相关标签:
3条回答
  • 2020-12-18 23:52

    For me it worked with the callback to display the results.

    import * as child from 'child_process';
    
     var foo: child.ChildProcess = child.exec('dir', (error: string, stdout: string, stderr: string) => {
                console.log(stdout);      
            });
    

    I didn't add any mappings in SystemJS as I dont have any such configuration in the node application

    0 讨论(0)
  • 2020-12-18 23:54

    If the error message is 'Cannot find module 'child_process' or its corresponding type declarations' the answer is 'npm install @types/watch'

    0 讨论(0)
  • 2020-12-19 00:07

    Ok, after some research #L138 I have found the solution

    You can use import as before

    import * as child from 'child_process';
    
    var foo: child.ChildProcess = child.exec('foo.sh');
    console.log(typeof foo.on);
    

    But you should configure SystemJS to map the module to NodeJS.

    System.config({
      map: {
        'child_process': '@node/child_process'
      }
    });
    

    That's it!

    0 讨论(0)
提交回复
热议问题