How does node enter the specified directory and execute the shell command?

I want to execute npm run build, in the specified directory through an API written by node, but the result reports an error. The reason is the directory where the npm run build is executed or the directory where the file is located. How to achieve the desired results?

process.exec("cd ", function (error, stdout, stderr) {
    process.exec("npm run build", function(error, stdout, stderr) {
        
    });
})
Mar.25,2021

in addition to the upstairs method, you can also process.chdir


1. You can use shell.js


process.exec('npm run build',{ cwd:'you script path' }, function(error, stdout, stderr) {
        
    });
Menu