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

  1. added
    child_processIssues and PRs related to the child_process subsystem.
    feature requestIssues requesting new Node.js features.
    on May 31, 2021
  2. Trott commented on May 31, 2021

    @Trott
    Member

    @nodejs/child_process

  3. bl-ue commented on Jun 2, 2021

    @bl-ue
    Contributor

    What if URL were an internet address like this?

    const myURL = new URL('/foo', 'https://example.org/');
    // https://example.org/foo

    Edit: see https://github.com/nodejs/node/pull/38862/files#diff-78a58a7046b99f3e5572d1d8a378139226f9963a5d6d7b51551c29f431285c89R72-R76

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