小tips:package.json设置环境变量

博客介绍了在package.json中设置环境变量的三种方法,需先安装cross - env。设置环境变量后,可在项目js脚本中通过process.env对象访问,结合webpack等打包软件实现不同代码逻辑,还给出了扩展参考。

三种方法可以在package.json设置环境变量。

先安装cross-env:

npm install --save-dev cross-env

{
  "scripts": {
    "dev1": "export WEBPACK_ENV=production && npx webpack -p",  ## mac
    "dev1": "set WEBPACK_ENV=production && npx webpack -p", ## windows
    "dev2": "cross-env CURRENT_ENV=development webpack-dev-server --inline --progress", ## 兼容所有平台
  }
}

设置环境变量的作用:

在项目的js脚本中,通过process.env这个对象就可以访问到设置的环境变量结合打包软件webpack等等,实现不同的代码逻辑:

console.log(process.env.WEBPACK_ENV)
console.log(process.env.CURRENT_ENV)

 扩展参考:阮一峰:npm scripts 使用指南

转载于:https://www.cnblogs.com/moqiutao/p/11172555.html

D:\project\SmallProgram\myApp>yarn dev:rn yarn run v1.22.22 $ npm run build:rn -- --watch > myApp@1.0.0 build:rn > taro build --type rn --watch 👽 Taro v4.1.1 Tips: 预览模式生成的文件较大,设置 NODE_ENV 为 production 可以开启压缩。 Example: $ set NODE_ENV=production && taro build --type rn --watch Installing React-Native related packages: react@^18.2.0 react-dom@^18.2.0 react-native@^0.73.1 @react-native/metro-config@^0.73.2 expo@~50.0.2 @tarojs/taro-rn @tarojs/components-rn @tarojs/rn-runner @tarojs/rn-supporter @tarojs/runtime-rn React-Native related packages have been installed successfully. ATTEHNTION: Package.json has been modified automatically, please submit it by yourself. index.js created WARNING: You should run npx react-native@latest to ensure you're always using the most current version of the CLI. NPX has cached version (0.73.11) != current release (0.79.3) info Welcome to React Native v0.73 info Starting dev server on port 8081... To print qrcode press "q" Welcome to Metro v0.80.12 Fast - Scalable - Integrated info React Native v0.79.3 is now available (your project is running on v0.73.11). info Changelog: https://github.com/facebook/react-native/releases/tag/v0.79.3 info Diff: https://react-native-community.github.io/upgrade-helper/?from=0.79.3 info For more info, check out "https://reactnative.dev/docs/upgrading?os=windows". info Dev server ready i - run on iOS a - run on Android d - open Dev Menu r - reload app info Opening app on Android... info A dev server is already running for this project on port 8081.
06-06
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值