Troubles with ch-15 \ lab 1
I feel like I understand the ask, but I've honestly tried multiple runs (I'm on windows 10 BTW).
For the spawnSync, I've tried, what I feel like are, all variations where I assign options to be:
{ env: { MY_ENV_VAR: myEnvVar }}
However, every time it runs, I get every process environment variable; including the value supplied from test.ts and injected into the index method argument of myEnvVar.
I honestly don't know if this is a windows environment issue with Node, or a bad test (not accounting for how it works with windows), or I am doing it wrong. Who knows, it could even be all of them ![]()
Please, what am I doing wrong here?
Thanks!
P.s. I tried to be short on the answer.
Comments
-
I just tried this out on a mac and I found out it passes. This lab should be re-evaluated for windows environments.
0 -
Hey @kellv thanks for feeding back. All of the labs were written to work on windows, but this one is especially tricky to validate as OS's have a habit of injecting env vars into child processes. I need more info about what's going wrong, but I suspect there's an env var being injected unexpectedly by Windows - this could even be happening based on a particular way your OS is configured.
are you using cmd or powershell?
would you mind modifying the child.js so that on line 2 (just under 'use strict') you have:
return console.log(process.env)
and then run the following command:
node -e "child_process.spawn(process.execPath, ['child.js'], {stdio: 'inherit', env: {}})"
and paste the output here?
0 -
Hi David,
Thanks for responding here.
The local setup on window is powershell using nvs use 12
When doing the argument injection I originally mentioned above, it is appending the property value to the already existing env vars object, which in itself is rather interesting.My findings were from altering the file by adding a console log for env similarly like you mentioned above as I was trying to debug my frustrations

I'll go back through and do the following above with your node -e execution later. (I'm back into studying mode again).
0 -
@davidmarkclements I just ran your command in powershell and it I get:
AssertionError [ERR_ASSERTION]: Expected values to be strictly equal: + actual - expected + undefined - 'is set' at Object.<anonymous> (C:\Users\kelly\jsnad\Labs-2020-06-07\ch-15\labs-1\child.js:9:8) at Module._compile (internal/modules/cjs/loader.js:1138:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10) at Module.load (internal/modules/cjs/loader.js:986:32) at Function.Module._load (internal/modules/cjs/loader.js:879:14) at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12) at internal/main/run_main_module.js:17:47 { generatedMessage: true, code: 'ERR_ASSERTION', actual: undefined, expected: 'is set', operator: 'strictEqual' }So. it looks like that works as expected
0 -
hey @kellv can you make sure to modify child.js so line two is exactly:
return console.log(process.env)
and then run the command
I want to exit early (return) and log out the env
0 -
As you officially requested sir. (and again, thank you)

{ HOMEDRIVE: 'C:', HOMEPATH: '\\Users\\kelly', LOGONSERVER: '\\\\DESKTOP-XXXXXX', PATH: 'C:\\Users\\kelly\\AppData\\Local\\nvs\\node\\12.18.0\\x64;C:\\Python38\\Scripts\\;C:\\Python38\\;C:\\WINDOWS\\system32;C:\\WINDOWS;C:\\WINDOWS\\System32\\Wbem;C:\\WINDOWS\ \System32\\WindowsPowerShell\\v1.0\\;C:\\WINDOWS\\System32\\OpenSSH\\;C:\\ProgramData\\chocolatey\\bin;C:\\Program Files\\dotnet\\;C:\\Program Files\\Git\\cmd;C:\\Users\\kelly\\App Data\\Local\\Microsoft\\WindowsApps;C:\\Users\\kelly\\AppData\\Local\\Programs\\Microsoft VS Code\\bin;C:\\Users\\kelly\\AppData\\Local\\JetBrains;C:\\Users\\kelly\\AppData\\Local\ \nvs\\', SYSTEMDRIVE: 'C:', SYSTEMROOT: 'C:\\WINDOWS', TEMP: 'C:\\Users\\kelly\\AppData\\Local\\Temp', USERDOMAIN: 'DESKTOP-XXXXXX', USERNAME: 'kelly', USERPROFILE: 'C:\\Users\\kelly', WINDIR: 'C:\\WINDOWS' }And to add, I get the same results running in powershell and cmd
0 -
Hey, got the same thing. Works on mac , not on windows. Error received: "child process should only have one env var".
1 -
hey @kellv to get that output did you run
node -e "child_process.spawn(process.execPath, ['child.js'], {stdio: 'inherit', env: {}})"
or
node child.js
0 -
Hello @davidmarkclements,
I'm struggling to get it on Windows too, BTW I edited
child.js'use strict' const assert = require('assert') const clean = (env) => Object.fromEntries( Object.entries(env).filter(([k]) => !/^(_.*|pwd|shlvl)/i.test(k)) ) const env = clean(process.env) console.log(env) assert.strictEqual(env.MY_ENV_VAR, 'is set') assert.strictEqual( Object.keys(env).length, 1, 'child process should have only one env var' ) console.log('passed!')Adding the
console.log(env)tried running it via the commandMINGW64 ~/Desktop/applications/certification/labs/ch-15/labs-1 $ node -e "child_process.spawn(process.execPath, ['child.js'], {stdio: 'inherit', env: { MY_ENV_VAR: 'is set' }})" { HOMEDRIVE: 'C:', HOMEPATH: '\\Users\\elaconico', LOGONSERVER: '...', MY_ENV_VAR: 'is set', PATH: 'C:\\Program Files\\Git\\mingw64\\bin;C:\\Program Files\\Git\\usr\\bin;C:\\Users\\elaconico\\bin;C:\\Program Files (x86)\\Common Files\\Oracle\\Java\\javapath;C:\\Python27\\;C:\\Python27\\Scripts;C:\\ProgramData\\DockerDesktop\\version-bin;C:\\Program Files\\Docker\\Docker\\Resources\\bin;C:\\WINDOWS\\system32;C:\\WINDOWS;C:\\WINDOWS\\System32\\Wbem;C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\;C:\\Program Files\\Microsoft VS Code\\bin;C:\\Program Files\\PuTTY\\;C:\\xampp\\php;C:\\composer;C:\\Program Files\\TortoiseSVN\\bin;C:\\Program Files\\Redis\\;C:\\Users\\elaconico\\AppData\\Roaming\\nvm;C:\\nodejs;C:\\Program Files (x86)\\GnuWin32\\bin;C:\\Program Files\\Git\\cmd;C:\\Program Files\\heroku\\bin;C:\\Ruby26-x64\\bin;C:\\Users\\elaconico\\AppData\\Local\\Microsoft\\WindowsApps;C:\\Program Files\\OpenVPN\\bin;C:\\Users\\elaconico\\AppData\\Roaming\\Composer\\vendor\\bin;C:\\Program Files\\MongoDB\\Server\\4.2\\bin;C:\\ffmpeg\\bin;C:\\Program Files\\WhoLockMe', SYSTEMDRIVE: 'C:', SYSTEMROOT: 'C:\\WINDOWS', TEMP: 'C:\\Users\\ELACON~1\\AppData\\Local\\Temp', USERDOMAIN: '...', USERNAME: 'elaconico', USERPROFILE: 'C:\\Users\\elaconico', WINDIR: 'C:\\WINDOWS' } assert.js:118 throw new AssertionError(obj); ^ AssertionError [ERR_ASSERTION]: child process should have only one env var at Object.<anonymous> (C:\Users\elaconico\Desktop\applications\certification\labs\ch-15\labs-1\child.js:11:8) at Module._compile (internal/modules/cjs/loader.js:1085:14) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10) at Module.load (internal/modules/cjs/loader.js:950:32) at Function.Module._load (internal/modules/cjs/loader.js:790:14) at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12) at internal/main/run_main_module.js:17:47 { generatedMessage: false, code: 'ERR_ASSERTION', actual: 12, expected: 1, operator: 'strictEqual' }blurred some of the value with
...
and yes, it seems it is injecting unwanted env vars, should we modify this then?Object.entries(env).filter(([k]) => !/^(_.*|pwd|shlvl)/i.test(k))Thank you for your support!
0 -
UPDATE
It seems to be a regex issue,
I tried modifying thecleanfunctionFROM
const clean = (env) => Object.fromEntries( Object.entries(env).filter(([k]) => !/^(_.*|pwd|shlvl)/i.test(k)) )
TO
const clean = (env) => Object.fromEntries( Object.entries(env).filter(([k]) => /^(MY_ENV_VAR)/i.test(k)) )
OUTPUT
MINGW64 ~/Desktop/applications/certification/labs/ch-15/labs-1 $ node -e "child_process.spawn(process.execPath, ['child.js'], {stdio: 'inherit', env: { MY_ENV_VAR: 'is set' }})" { MY_ENV_VAR: 'is set' } passed!tried intentionally to make it not work
const clean = (env) => Object.fromEntries( Object.entries(env).filter(([k]) => /^(MY_ERROR_VAR)/i.test(k)) )
OUTPUT
MINGW64 ~/Desktop/applications/certification/labs/ch-15/labs-1 $ node -e "child_process.spawn(process.execPath, ['child.js'], {stdio: 'inherit', env: { MY_ENV_VAR: 'is set' }})" {} assert.js:118 throw new AssertionError(obj); ^ AssertionError [ERR_ASSERTION]: Expected values to be strictly equal: + actual - expected + undefined - 'is set' at Object.<anonymous> (C:\Users\elaconico\Desktop\applications\certification\labs\ch-15\labs-1\child.js:10:8) at Module._compile (internal/modules/cjs/loader.js:1085:14) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10) at Module.load (internal/modules/cjs/loader.js:950:32) at Function.Module._load (internal/modules/cjs/loader.js:790:14) at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12) at internal/main/run_main_module.js:17:47 { generatedMessage: true, code: 'ERR_ASSERTION', actual: undefined, expected: 'is set', operator: 'strictEqual' }Hope this helps.
0 -
I had the same issue, switched to Ubuntu WSL to pass
0 -
@elaconico - unfortunately your changes just lead to asserting that true is true, we don't just want MY_ENV_VAR to be set, we want it to to be the ONLY env var that is set
if you could console log
envin the clean function and post results here I can take a look at seeing what else is in the way here0 -
I almost managed to ovewrite the environment variables but on the
assertioni get another one variable, apart from the one that we need.I create
an options object, where i add theenv variablethat we need:excersice(myEnvVar){ const exec_options = { cwd: null, env: { MY_ENV_VAR: myEnvVar }, encoding: 'utf8', timeout: 0, maxBuffer: 200 * 1024 } const childprocess = spawn(process.execPath, ['-p', 'process.env'], exec_options) return childprocess }But the output that i get on the
node testhas one more env variable. Can someone give me a clue why this__CF_USER_TEXT_ENCODING:is added on its own ?
0 -
all good now, i added the
{stdio: inherit}on the exec_options, and added thechild.js, and all worked.const exec_options = { env: { MY_ENV_VAR: myEnvVar }, stdio: 'inherit' } const childprocess = spawn(process.execPath, ['child.js'], exec_options)0 -
I run all labs code based on Docker without issues and my host machine is Windows too.
0 -
@krave that makes sense because Docker is generally linux, so really you're running the code on linux.
0 -
Aha, really love to write code based on docker now.
Thanks to WSL2 and VScode remote extensions.
0 -
nice
0 -
Since child process inherits parent process.env by default, I just did
process.env = { ... }beforespawn().0 -
@doruk what we're trying to demonstrate here is that the env option passed to spawn creates a clean env. On Windows, that's not entirely true (but it mostly is). So while your approach will cause you to pass the item, it's obviously not a good idea to clear all of process env in the parent process in real-world scenarios.
0
Categories
- All Categories
- 158 LFX Mentorship
- 158 LFX Mentorship: Linux Kernel
- 850 Linux Foundation IT Professional Programs
- 389 Cloud Engineer IT Professional Program
- 186 Advanced Cloud Engineer IT Professional Program
- 88 DevOps Engineer IT Professional Program
- 156 Cloud Native Developer IT Professional Program
- 152 Express Training Courses & Microlearning
- 149 Express Courses - Discussion Forum
- 3 Microlearning - Discussion Forum
- 7.2K Training Courses
- 50 LFC110 Class Forum - Discontinued
- 74 LFC131 Class Forum - DISCONTINUED
- 56 LFD102 Class Forum
- 258 LFD103 Class Forum
- LFD103-JP クラス フォーラム
- 27 LFD110 Class Forum
- 50 LFD121 Class Forum
- 3 LFD123 Class Forum
- 1 LFD125 Class Forum
- 19 LFD133 Class Forum
- 10 LFD134 Class Forum
- 19 LFD137 Class Forum
- 1 LFD140 Class Forum
- 73 LFD201 Class Forum
- 8 LFD210 Class Forum
- 6 LFD210-CN Class Forum
- 2 LFD213 Class Forum - Discontinued
- LFD221 Class Forum
- 128 LFD232 Class Forum - Discontinued
- 3 LFD233 Class Forum - Discontinued
- 5 LFD237 Class Forum
- 25 LFD254 Class Forum
- 750 LFD259 Class Forum
- 111 LFD272 Class Forum - Discontinued
- 4 LFD272-JP クラス フォーラム - Discontinued
- 16 LFD273 Class Forum
- 477 LFS101 Class Forum
- 4 LFS111 Class Forum
- 4 LFS112 Class Forum
- 5 LFS116 Class Forum
- 9 LFS118 Class Forum
- 2 LFS120 Class Forum
- LFS140 Class Forum
- 12 LFS142 Class Forum
- 9 LFS144 Class Forum
- 6 LFS145 Class Forum
- 6 LFS146 Class Forum
- 7 LFS147 Class Forum
- 21 LFS148 Class Forum
- 17 LFS151 Class Forum
- 6 LFS157 Class Forum
- 91 LFS158 Class Forum
- 1 LFS158-JP クラス フォーラム
- 14 LFS162 Class Forum
- 2 LFS166 Class Forum - Discontinued
- 9 LFS167 Class Forum
- 5 LFS170 Class Forum
- 2 LFS171 Class Forum - Discontinued
- 4 LFS178 Class Forum - Discontinued
- 4 LFS180 Class Forum
- 3 LFS182 Class Forum
- 7 LFS183 Class Forum
- 2 LFS184 Class Forum
- 41 LFS200 Class Forum
- 737 LFS201 Class Forum - Discontinued
- 3 LFS201-JP クラス フォーラム - Discontinued
- 23 LFS203 Class Forum
- 141 LFS207 Class Forum
- 3 LFS207-DE-Klassenforum
- 3 LFS207-JP クラス フォーラム
- 302 LFS211 Class Forum - Discontinued
- 56 LFS216 Class Forum - Discontinued
- 60 LFS241 Class Forum
- 51 LFS242 Class Forum
- 39 LFS243 Class Forum
- 17 LFS244 Class Forum
- 8 LFS245 Class Forum
- 1 LFS246 Class Forum
- 1 LFS248 Class Forum
- 124 LFS250 Class Forum
- 3 LFS250-JP クラス フォーラム
- 2 LFS251 Class Forum - Discontinued
- 163 LFS253 Class Forum
- 1 LFS254 Class Forum - Discontinued
- 3 LFS255 Class Forum
- 15 LFS256 Class Forum
- 2 LFS257 Class Forum
- 1.4K LFS258 Class Forum
- 12 LFS258-JP クラス フォーラム
- 142 LFS260 Class Forum
- 165 LFS261 Class Forum
- 45 LFS262 Class Forum
- 82 LFS263 Class Forum - Discontinued
- 15 LFS264 Class Forum - Discontinued
- 11 LFS266 Class Forum - Discontinued
- 25 LFS267 Class Forum
- 27 LFS268 Class Forum
- 38 LFS269 Class Forum
- 11 LFS270 Class Forum
- 202 LFS272 Class Forum - Discontinued
- 2 LFS272-JP クラス フォーラム - Discontinued
- 2 LFS274 Class Forum - Discontinued
- 4 LFS281 Class Forum - Discontinued
- 30 LFW111 Class Forum
- 265 LFW211 Class Forum
- 190 LFW212 Class Forum
- 17 SKF100 Class Forum
- 2 SKF200 Class Forum
- 3 SKF201 Class Forum
- 800 Hardware
- 200 Drivers
- 68 I/O Devices
- 37 Monitors
- 104 Multimedia
- 175 Networking
- 92 Printers & Scanners
- 85 Storage
- 765 Linux Distributions
- 82 Debian
- 67 Fedora
- 20 Linux Mint
- 13 Mageia
- 23 openSUSE
- 149 Red Hat Enterprise
- 31 Slackware
- 13 SUSE Enterprise
- 356 Ubuntu
- 472 Linux System Administration
- 39 Cloud Computing
- 71 Command Line/Scripting
- Github systems admin projects
- 96 Linux Security
- 78 Network Management
- 102 System Management
- 48 Web Management
- 74 Mobile Computing
- 19 Android
- 42 Development
- 1.2K New to Linux
- 1K Getting Started with Linux
- 388 Off Topic
- 121 Introductions
- 178 Small Talk
- 28 Study Material
- 879 Programming and Development
- 312 Kernel Development
- 549 Software Development
- 1.8K Software
- 270 Applications
- 183 Command Line
- 5 Compiling/Installing
- 988 Games
- 320 Installation
- 109 All In Program
- 109 All In Forum
Upcoming Training
-
August 20, 2018
Kubernetes Administration (LFS458)
-
August 20, 2018
Linux System Administration (LFS301)
-
August 27, 2018
Open Source Virtualization (LFS462)
-
August 27, 2018
Linux Kernel Debugging and Security (LFD440)

