在跨平台的npm脚本中使用环境变量 [英] Using environment variables in npm scripts across platforms

查看:155
本文介绍了在跨平台的npm脚本中使用环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建package.json并使用"npm run"运行一些脚本,确切地说, https://docs.npmjs.com/misc/scripts .

I am building a package.json and use "npm run" to run some scripts, to be exactly, https://docs.npmjs.com/misc/scripts.

我的脚本需要扩展一些环境变量,我想使其跨平台兼容.例如,我的脚本会说

My script would need to expand some environment variables and I want to make it cross platform compatible. For example, my script would say

"scripts": {
  "build": "md %npm_package_version%\helloworld"
}

但是由于环境变量的扩展,它当前正在Windows上运行. Linux将使用md $npm_package_version\helloworld.

But it's currently running on Windows because the expansion of environment variables. Linux would use md $npm_package_version\helloworld.

npm是否带有转换环境变量扩展的机制,以便它可以跨平台工作?

Does npm comes with a mechanism to convert environment variables expansion so that it works across platforms?

推荐答案

要使其跨平台,请使用

To make it cross-platform, use cross-var:

"scripts": {
  "build": "cross-var md %npm_package_version%\helloworld"
}

这篇关于在跨平台的npm脚本中使用环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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