最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - electron-packager spawn ENOENT - Stack Overflow

programmeradmin2浏览0评论

I'm working on an electron app that controls gulp tasks via gui. You click on a task and it runs. Pretty simple stuff. On macOS, when I run npm start it runs just fine, but when I package it with electron-packager, I get this error:

Uncaught Exception:
Error: spawn gulp ENOENT
    at exports._errnoException (util.js:1022:11)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:193:32)
    at onErrorNT (internal/child_process.js:359:16)
    at _binedTickCallback (internal/process/next_tick.js:74:11)
    at process._tickCallback (internal/process/next_tick.js:98:9)

This is the code:

exports.runTask = (taskName, projPath) => {
    const cp = spawn('gulp', [ taskName ], {cwd: projPath});
    cp.stdout.setEncoding('utf8');
    cp.stdout.on('data', data => {
        console.log(data);
        mainWindow.webContents.send('task-console-data', data);
    });

    cp.stderr.setEncoding('utf8');
    cp.stderr.on('data', data => {
        console.error(data);
        displayNotification({text: `[error] ${data}`});
        mainWindow.webContents.send('task-console-data', `[error] ${data}`);
    });

    cp.on('exit', code => {
        if (code === 0) {
          displayNotification({
            title: 'gulp',
            subtitle: 'Finished running tasks'
          });
        } else if ( !code || code === null ) {
            return;
        } else {
            console.error(`Exited with error code ${code}`);

            displayNotification({
                title: 'gulp',
                subtitle: `Exited with error code ${code}`,
                sound: 'Basso'
            });
        }
    });
};

I'm working on an electron app that controls gulp tasks via gui. You click on a task and it runs. Pretty simple stuff. On macOS, when I run npm start it runs just fine, but when I package it with electron-packager, I get this error:

Uncaught Exception:
Error: spawn gulp ENOENT
    at exports._errnoException (util.js:1022:11)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:193:32)
    at onErrorNT (internal/child_process.js:359:16)
    at _binedTickCallback (internal/process/next_tick.js:74:11)
    at process._tickCallback (internal/process/next_tick.js:98:9)

This is the code:

exports.runTask = (taskName, projPath) => {
    const cp = spawn('gulp', [ taskName ], {cwd: projPath});
    cp.stdout.setEncoding('utf8');
    cp.stdout.on('data', data => {
        console.log(data);
        mainWindow.webContents.send('task-console-data', data);
    });

    cp.stderr.setEncoding('utf8');
    cp.stderr.on('data', data => {
        console.error(data);
        displayNotification({text: `[error] ${data}`});
        mainWindow.webContents.send('task-console-data', `[error] ${data}`);
    });

    cp.on('exit', code => {
        if (code === 0) {
          displayNotification({
            title: 'gulp',
            subtitle: 'Finished running tasks'
          });
        } else if ( !code || code === null ) {
            return;
        } else {
            console.error(`Exited with error code ${code}`);

            displayNotification({
                title: 'gulp',
                subtitle: `Exited with error code ${code}`,
                sound: 'Basso'
            });
        }
    });
};
Share Improve this question asked Jul 17, 2017 at 16:16 Dino MorrisonDino Morrison 1072 silver badges8 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

In case anyone else is running into this problem, the answer is to correct the $PATH. There is a package on npm that does this for you.

https://www.npmjs./package/fix-path

You should forward the process.env.PATH to the spawn options because the child process environment variables is overriding in options.env. So the OS don't know exactly where to look your mand gulp.

So you can do this:

const cp = spawn('gulp', [ taskName ], {
    cwd: projPath,
    env: {
        PATH: process.env.PATH,
    },
});
发布评论

评论列表(0)

  1. 暂无评论