Skip to content

Let child_process 'cwd' arguments accept a URL #38861

Closed
@thernstig

Description

@thernstig

Is your feature request related to a problem? Please describe.
It would be nice if child_process functionality, such as exec, could accept a URL (docs) for the cwd argument. For example the File System writeFile accepts a URL as its file argument already. So it would be nice for the child_process module to do so as well for path arguments.

This is especially nice with ECMAScript modules since e.g. the old __dirname does not exist anymore, but we are forced to use URL to get a file path to the current file.

Describe the solution you'd like

// Using ESM Module syntax:
import { exec } from 'child_process/promises';

try {
  const { stdout } = await exec(
    'sysctl -n net.ipv4.ip_local_port_range',
    { encoding: 'utf8', cwd: new URL('../../', import.meta.url) } // without URL support we would have to have used .pathname here
  );
  console.log('successfully executed the child process command');
} catch (error) {
  console.error('there was an error:', error.message);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    child_processIssues and PRs related to the child_process subsystem.feature requestIssues that request new features to be added to Node.js.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions