Skip to content

Let child_process 'cwd' arguments accept a URL #38861

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);
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    child_processIssues and PRs related to the child_process subsystem.feature requestIssues requesting new Node.js features.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions