Is there any way how to run “nvm use” automatically in “prestart” npm script?

前端 未结 2 466
抹茶落季
抹茶落季 2020-12-30 06:31

I would like to have automatically invoke \"nvm use\" when I run \"npm start\". So I come up with this solution:

package.json

\"scripts\": {
\"presta         


        
相关标签:
2条回答
  • 2020-12-30 06:46

    Generally on a Mac, the nvm.sh file is located in your home path. Use the $HOME variable if you have multiple Mac users working on the code.

    "scripts": {
        "prestart": "source $HOME/.nvm/nvm.sh; nvm use"
    }
    

    I would've added this as a comment to the above response, but I'm not allowed :(

    0 讨论(0)
  • 2020-12-30 07:05

    Your package.json could look like

    "scripts": {
    "start": "source /whereever/located/nvm.sh; nvm use; nodemon index.js"
    }
    

    To explain. The "start" line is a single shell instance. So you have to have nvm initialize the PATH in that shell instance. Also, nvm is a shell function not an executable shell script. The nvm function lives in the shell instance, and is created by sourcing the nvm.sh file.

    Sorry for the edits cuz I didn't test my first two.

    0 讨论(0)
提交回复
热议问题