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

前端 未结 2 476
抹茶落季
抹茶落季 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 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.

提交回复
热议问题