有没有一种方法可以运行"nvm use"?自动在prestart npm脚本中? [英] Is there a way to run "nvm use" automatically in a prestart npm script?

查看:64
本文介绍了有没有一种方法可以运行"nvm use"?自动在prestart npm脚本中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想自动调用"nvm use"当我运行"npm start"时.所以我想出了这个解决方案:

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

package.json文件

"scripts": {
  "prestart": "sh test.sh",
  "start": "nodemon index.js"
}

.nvmrc文件

4

test.sh文件

#!/bin/bash

if [ -d ~/.nvm ]
  then
    source ~/.nvm/nvm.sh

    nvm use
fi

这可行,并且在nvm版本之间切换,控制台输出为:

This works and switches between nvm versions console output is:

> sh test.sh

Found '/my-user-path/.nvmrc' with version <4>
Now using node v4.2.2 (npm v2.14.7)

> app@1.0.0 start /app-path/
> nodemon index.js

但是当我将index.js表单称为"console.log(process.versions);"时,nvm脚本可能在不同的过程中执行,因此输出为:

But when I call form index.js "console.log(process.versions);" nvm script is executed probably in different process so output is:

{ 
  http_parser: '2.6.0',
  node: '5.1.0',
  v8: '4.6.85.31',
  uv: '1.7.5',
  zlib: '1.2.8',
  ares: '1.10.1-DEV',
  icu: '56.1',
  modules: '47',
  openssl: '1.0.2d' 
}

关于如何正确处理此问题的任何建议?

Any suggestions on how to deal with this in a proper way?

谢谢

推荐答案

通常在Mac上,nvm.sh文件位于您的主路径中.如果您有多个Mac用户使用该代码,请使用$ HOME变量.

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 :(

这篇关于有没有一种方法可以运行"nvm use"?自动在prestart npm脚本中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆