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

child_process.spawn() requires "/" but path.resolve() returns "\" on windows #3116

Closed
SpikedKira opened this issue Sep 29, 2015 · 9 comments
Closed
Labels
child_process Issues and PRs related to the child_process subsystem. windows Issues and PRs related to the Windows platform.

Comments

@SpikedKira
Copy link

ERRORS:
var mypath = path.resolve('node_modules', 'package', 'scriptName.cmd');
var cp = child_process.spawn(mypath, args);

WORKS:
var mypath = path.resolve('node_modules', 'package', 'scriptName.cmd');
mypath = mypath.replace(//g, '/');
var cp = child_process.spawn(mypath, args);

@silverwind silverwind added child_process Issues and PRs related to the child_process subsystem. windows Issues and PRs related to the Windows platform. labels Sep 29, 2015
@bricss
Copy link

bricss commented Sep 29, 2015

@jhamhader
Copy link
Contributor

What error does it produce?

@SpikedKira
Copy link
Author

oh man, i didn't document the error, but if I recall, spawn() tried to digest the path up to the first "" and dropped the rest which resulted in a path not found or similar

@dotnetCarpenter
Copy link

I'm investigating a very similar case where spawn throws Error: spawn UNKNOWN when getting a path from require.resolve().

E.i: t1.js

require("child_process").spawn( require.resolve("./t2") )

t2.js

#!/usr/bin/env node
console.log("Hello World");

path.normalize does not make any difference. This is a Windows only issue which makes me think that spawn behaves very differently on Windows.

@dotnetCarpenter
Copy link

After reading the an old issue in node 0.6 I figure that the shebang I use saved me on mac and linux but became an issue on windows. The work around for Windows's missing shebang support is add path as a arguments to nodejs.

require("child_process").spawn( process.execPath, [require.resolve("./t2")] )

@cjihrig
Copy link
Contributor

cjihrig commented Feb 16, 2016

@dotnetCarpenter if you use the latest code from master, you can do:

'use strict';
const cp = require('child_process');
const file = require.resolve('./foo');
const child = cp.spawn(file, {shell: true});

child.stdout.setEncoding('utf8');
child.stdout.on('data', console.log);

I tested this on Windows 7 and OS X. Node handles the shebang for you on all platforms. The only catch is that on Windows, the .js extension must be associated with Node, and on other operating systems the file must have executable permissions.

@cjihrig cjihrig closed this as completed Feb 16, 2016
@dotnetCarpenter
Copy link

The only catch is that on Windows, the .js extension must be associated with Node, and on other operating systems the file must have executable permissions.
...
Next you need to tell Windows to use node.exe instead of wscript.exe (Anyone still use that?) You'll need a CMD.exe window with Administrator privileges. Easy way to do that is to tap the 'Windows' key, type in CMD, right click when it appears in the menu and click 'Run as Administrator'. You may need a password.

@cjihrig well, that gotcha makes nodejs code unportable.
To be able to run nodejs crossplatform we really need the following to work consistently on OS X, linux and Windows:

'use strict';
const cp = require('child_process');
const file = require.resolve('./foo');
const child = cp.spawn(file, {nodejs: true}); // tell child_process this is a nodejs executable

child.stdout.setEncoding('utf8');
child.stdout.on('data', console.log);

Please reopen.

@bnoordhuis
Copy link
Member

@dotnetCarpenter cp.spawn(process.execPath, [file])? I don't see why you'd need anything more.

@dotnetCarpenter
Copy link

@bnoordhuis You're completly right. I somehow forgot that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
child_process Issues and PRs related to the child_process subsystem. windows Issues and PRs related to the Windows platform.
Projects
None yet
Development

No branches or pull requests

7 participants