有没有办法如何运行“nvm use”自动地在“预启动”中npm脚本? [英] Is there any way how to run "nvm use" automatically in "prestart" npm script?

查看:126
本文介绍了有没有办法如何运行“nvm use”自动地在“预启动”中npm脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

package.json

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 switch 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

但是当我调用form 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 how to deal with this in 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”自动地在“预启动”中npm脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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