Welcome to the Linux Foundation Forum!

Ch-15 - spawn / process.env

Options

Hi, when I run the following code

'use strict'
const { spawn } = require('child_process')

const sp = spawn(process.execPath, ['-p', 'process.env'], {
  env: {SUBPROCESS_SPECIFIC: 'ENV VAR'}
})

sp.stdout.pipe(process.stdout)
console.log('total var::', Object.keys(process.env).length)

I get the result

total var:: 39
{
  SUBPROCESS_SPECIFIC: 'ENV VAR',
  __CF_USER_TEXT_ENCODING: '0x1F5:0x0:0xF'
}

My question is

  1. what and why is __CF_USER_TEXT_ENCODING: '0x1F5:0x0:0xF' in the outout? is this expected?
  2. the total length of process.env is 39. is this correct or it should be 2?

I am assuming it still got other environment variables in the process because when I console.log(process.env) I see heaps of them.

What am I doing wrong ?

Thanks

Comments

  • davidmarkclements
    Options

    hi @hafeez-syed

    No this all looks okay - this isn't a lab exercise, you're playing with the some of the example code.
    The __CF_USER_TEXT_ENCODING is injected by macOS - no matter what. It's annoying. If you look at the validate.js file of the labs for this chapter you'll see it's stripped out on macOS systems.

    The count is 39 because you apparently have 39 environment variables set, you're logging out the parent env not the child env here.

    If you just type env on your command line, you'll see you have a bunch of system environment variables set. That's what you're counting on the last line of you code here.

Categories

Upcoming Training