首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在JS中设置环境变量?

如何在JS中设置环境变量?
EN

Stack Overflow用户
提问于 2018-10-19 08:05:08
回答 12查看 54.6K关注 0票数 21

我对JS的反应是新的。我正在尝试从React构建war文件,但是被困在下面的某个地方。它给了我以下的错误。

代码语言:javascript
复制
Creating an optimized production build...

Treating warnings as errors because process.env.CI = true.
Most CI servers set it automatically.

Failed to compile.



./src/Home.js
  Line 2:   'AppNavbar' is defined but never used  no-unused-vars
  Line 3:  'Link' is defined but never used       no-unused-vars
  Line 4:  'Button' is defined but never used     no-unused-vars
  Line 4:  'Container' is defined but never used  no-unused-vars

./src/App.js
  Line 5:   'MenuBar' is defined but never used        no-unused-vars
  Line 6:   'PrivilegeList' is defined but never used  no-unused-vars
  Line 8:   'logo' is defined but never used           no-unused-vars


  npm ERR! code ELIFECYCLE
  npm ERR! errno 1
  npm ERR! my-app@0.1.0 build: `react-scripts build`
  npm ERR! Exit status 1
  npm ERR!
  npm ERR! Failed at the my-app@0.1.0 build script.
  npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

  npm ERR! A complete log of this run can be found in:
    npm ERR!     D:\ReactJS-workspace\my-app\npm\cache\_logs\2018-10-19T07_44_19_233Z-debug.log
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD FAILURE
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 01:36 min
    [INFO] Finished at: 2018-10-19T13:14:19+05:30
    [INFO] ------------------------------------------------------------------------
    [ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.3.2:exec (npm run build (compile)) on project my-app: Command execution failed.: Process exited with an error: 1 (Exit value: 1) -> [Help 1]
    [ERROR]
    [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
    [ERROR] Re-run Maven using the -X switch to enable full debug logging.
    [ERROR]
    [ERROR] For more information about the errors and possible solutions, please read the following articles:
    [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

下面是我的文件夹结构。

我想设置process.env.CI = false如何设置环境变量中的JS?

EN

回答 12

Stack Overflow用户

回答已采纳

发布于 2019-01-08 06:12:52

要将其设置为当前流程执行,只需编辑package.json文件并修改“构建”脚本,如下所示:

代码语言:javascript
复制
"scripts": {
"start": "react-scripts start",
"build": "set \"CI=false\" && react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject" }

这将把CI环境变量设置为"false“。现在,您可以使用CI变量集执行build命令:

代码语言:javascript
复制
npm run build
票数 20
EN

Stack Overflow用户

发布于 2018-10-19 08:15:15

看看这个包裹多滕诺夫

  1. 在工作目录中创建一个新文件.env
  2. 通过dotenv安装npm install dotenv
  3. 将此添加到应用程序require('dotenv').config()
  4. 在那个文件中写process.env.CI = false
  5. 如果使用git,则将.env添加到.gitignore
  6. 重新启动应用程序。

或者运行这个CI=false npm run build

票数 12
EN

Stack Overflow用户

发布于 2019-04-17 09:19:36

你的问题标题与描述中所发生的非常不同。

要在React中使用环境变量,它们必须以REACT_APP_作为前缀。

例如,以下内容将由React应用程序获取:

REACT_APP_API_URL=/api

但这不会:

API_URL=/api

如需更多资料,请参阅正式文件:

票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52888214

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档