Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

process.env取值问题 #8818

Closed
superNos opened this issue Mar 5, 2021 · 2 comments
Closed

process.env取值问题 #8818

superNos opened this issue Mar 5, 2021 · 2 comments
Labels
F-react Framework - React T-weapp Target - 编译到微信小程序 V-3 Version - 3.x

Comments

@superNos
Copy link

superNos commented Mar 5, 2021

相关平台

微信小程序

小程序基础库: 2.12.0
使用框架: React

复现步骤

pacage.json内scripts使用cross-env 传递 env_type字段 为test, config/index.js内配置env_type为prod
在项目自身代码中process.env.env_type取值正常为test
在node_modules的包内使用process.env.env_type取值却为prod

// package.json
"scripts": {
     "build": "cross-env env_type=test taro build --type weapp"
}
// config/index.js
env: {
   env_type: 'prod'
}

期望结果

node_modules和项目自身代码使用process.env取值一致

实际结果

项目自身代码process.env值为cross-env传递的值
node_modules内为config/index.js配置的值

环境信息

👽 Taro v3.0.27


  Taro CLI 3.0.27 environment info:
    System:
      OS: macOS 10.14.6
      Shell: 5.3 - /bin/zsh
    Binaries:
      Node: 12.10.0 - /usr/local/bin/node
      Yarn: 1.13.0 - /usr/local/bin/yarn
      npm: 6.10.3 - /usr/local/bin/npm
    npmPackages:
      @tarojs/cli: 3.0.27 => 3.0.27 
      @tarojs/components: 3.0.27 => 3.0.27 
      @tarojs/mini-runner: 3.0.27 => 3.0.27 
      @tarojs/react: 3.0.27 => 3.0.27 
      @tarojs/runtime: 3.0.27 => 3.0.27 
      @tarojs/taro: 3.0.27 => 3.0.27 
      @tarojs/webpack-runner: 3.0.27 => 3.0.27 
      babel-preset-taro: 3.0.27 => 3.0.27 
      eslint-config-taro: 3.0.27 => 3.0.27 
      nerv-devtools: ^1.5.7 => 1.5.7 
      nervjs: ^1.5.7 => 1.5.7 
      react: 16.10.0 => 16.10.0 
      taro-ui: ^3.0.0-alpha.2 => 3.0.0-alpha.3
@taro-bot2 taro-bot2 bot added F-react Framework - React T-weapp Target - 编译到微信小程序 V-3 Version - 3.x labels Mar 5, 2021
@Chen-jj
Copy link
Contributor

Chen-jj commented Mar 8, 2021

config/index.jsenv 会通过 Webpack definePlugin 设置。

可以试试:

// config/index.js
env: {
   env_type: process.env.env_type || 'prod'
}

@superNos
Copy link
Author

superNos commented Mar 9, 2021

config/index.jsenv 会通过 Webpack definePlugin 设置。

可以试试:

// config/index.js
env: {
   env_type: process.env.env_type || 'prod'
}

目前这样解决了

@Chen-jj Chen-jj closed this as completed Mar 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
F-react Framework - React T-weapp Target - 编译到微信小程序 V-3 Version - 3.x
Projects
None yet
Development

No branches or pull requests

2 participants