Monday, August 22, 2022

[FIXED] How to resolve a path that includes an environment variable, in nodejs?

Issue

I would like to run an executable and its path contains an enviroment variable, for example if I would like to run chrome.exe I would like to write something like this

var spawn = require('child_process').spawn;
spawn('chrome',[], {cwd: '%LOCALAPPDATA%\\Google\\Chrome\\Application', env: process.env})

instead of

var spawn = require('child_process').spawn;
spawn('chrome',[], {cwd: 'C:\\Users\myuser\\AppData\\Local\\Google\\Chrome\\Application', env: process.env}).

Is there a package I can use in order to achieve this?


Solution

These answers are crazy. Just can use path:

const folder = require('path').join(
    process.env.LOCALAPPDATA,
    'Google/Chrome/Application',
);

console.log(folder); // C:\Users\MyName\AppData\Local\Google\Chrome\Application


Answered By - ZYinMD
Answer Checked By - David Goodson (PHPFixing Volunteer)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.