Change working directory in my current shell context when running Node script

前端 未结 4 1909
暖寄归人
暖寄归人 2021-01-31 00:49

I am trying to change the working directory of my Node.js script when it is run from a bin script. I have something like the following:

#!/usr/bin/env node
proce         


        
4条回答
  •  广开言路
    2021-01-31 01:21

    There is no built-in method for Node to change the CWD of the underlying shell running the Node process.

    You can change the current working directory of the Node process through the command process.chdir().

    var process = require('process');
    process.chdir('../');
    

    When the Node process exists, you will find yourself back in the CWD you started the process in.

提交回复
热议问题