Welcome to the Linux Foundation Forum!

Lab 15.1 problem. Path to child.js

Hello, I think Lab 15.1 have a problem, or I think is a problem.
In the lab you have this code:

'use strict'
function exercise(myEnvVar) {
  // TODO return a child process with
  // a single environment variable set 
  // named MY_ENV_VAR. The MY_ENV_VAR 
  // environment variable's value should 
  // be the value of the myEnvVar parameter 
  // passed to this exercise function

  return require('child_process').spawnSync(process.argv[0], ['child.js'])
}

module.exports = exercise

I code that the exercise require you, but it was throwing me an error: 'child.js file not found'

So i need to change the template code to

'use strict'

function exercise(myEnvVar) {
  // TODO return a child process with
  // a single environment variable set 
  // named MY_ENV_VAR. The MY_ENV_VAR 
  // environment variable's value should 
  // be the value of the myEnvVar parameter 
  // passed to this exercise function


  return require('child_process').spawnSync(process.argv[0], ['./ch-15/labs-1/child.js'], {
    env: { MY_ENV_VAR: myEnvVar }
  })
}

module.exports = exercise


And then it passed.
I was doing right the exercise before i changed the route to child.js.
If I see some template code in the exercises I suppose i dont need to modify it.
Lost bit of time cause this problem, in my opinion this exercise shouldnt have template code.

Comments

  • I'm stuck on this one but Im 'child process should only have one env var' and also 'exit code should be 0'.

    'use strict'

    function exercise (myEnvVar) {
    // TODO return a child process with
    // a single environment variable set
    // named MY_ENV_VAR. The MY_ENV_VAR
    // environment variable's value should
    // be the value of the myEnvVar parameter
    // passed to this exercise function
    return require('child_process').spawnSync(process.argv[0], ['child.js'], {
    env: { MY_ENV_VAR: myEnvVar } }
    )
    }

    module.exports = exercise

  • @xwarzdev - did you run validate.js from a parent folder? The 'child.js' passed to spawnSync is relative to the current working directory. If you wanted to fix that so you could run it from anywhere you could do:

    'use strict'
    const { join } = require('path')
    function exercise(myEnvVar) {
      // TODO return a child process with
      // a single environment variable set 
      // named MY_ENV_VAR. The MY_ENV_VAR 
      // environment variable's value should 
      // be the value of the myEnvVar parameter 
      // passed to this exercise function
    
      return require('child_process').spawnSync(process.argv[0], [join(__dirname, 'child.js')])
    }
    
    module.exports = exercise
    
  • @olmryoung are you on Windows? If so are you using cmd.exe or powershell or something else?

  • Windows powershell

  • @olmryoung ok so you have the correct answer, it's just that the validate.js file doesn't cover for every possibility on Windows. Powershell is probably injecting extra environment variables into the the child process.

  • Same here, windows add extra environnement variables. I run this code on linux and max and it's OK

Categories

Upcoming Training