简体   繁体   English

如何提供 `babel-preset-react-app` 环境变量?

[英]How to provide `babel-preset-react-app` env variables?

I am working on an App which connects create-react-app with an express server( using server rendering).我正在开发一个将 create-react-app 与快速服务器(使用服务器渲染)连接起来的应用程序。 I am referring this tutorial for it.To render the file from server, the code is我指的是本教程。要从服务器呈现文件,代码是

bootstrap.js引导程序.js

require('ignore-styles');
require('babel-register')({
ignore:[/(node-modules)/],
presets:['es2015','react-app']
});
require('./index');

index.js index.js

import express from 'express';
// we'll talk about this in a minute:
import serverRenderer from './middleware/renderer';


const PORT = 3000;
const path = require('path');
// initialize the application and create the routes
const app = express();
const router = express.Router();
// root (/) should always serve our server rendered page
router.use('^/$', serverRenderer);
// other static resources should just be served as they are
router.use(express.static(
    path.resolve(__dirname, '..', 'build'),
    { maxAge: '30d' },
));
// tell the app to use the above rules
app.use(router);
// start the app
app.listen(PORT, (error) => {
    if (error) {
        return console.log('something bad happened', error);
    }

    console.log("listening on " + PORT + "...");
});

While running the command运行命令时

node bootstrap.js

I am getting error that我收到错误消息

Error: Using babel-preset-react-app requires that you specify NODE_ENV or BABEL_ENV environment variables.错误:使用babel-preset-react-app需要您指定NODE_ENVBABEL_ENV环境变量。 Valid values are "development", "test", and "production".有效值为“开发”、“测试”和“生产”。

There are a few options here.这里有几个选项。 I will describe the most easy options.我将描述最简单的选项。

The most easy one is to run your node bootstrap.js like this:最简单的方法是像这样运行您的节点 bootstrap.js:

NODE_ENV=production BABEL_ENV=production node bootstrap.js

But that is just too long to remember every time, so you can use package.json scripts.但这太长了,每次都记不住,所以你可以使用 package.json 脚本。

If you open up your package.json file, you should see a scripts section (if not, see the doc ).如果您打开 package.json 文件,您应该会看到一个脚本部分(如果没有,请参阅文档)。 In that scripts section you can create your own scripts.在该脚本部分,您可以创建自己的脚本。

I mostly use 2 scripts, one for development and one for production.我主要使用 2 个脚本,一个用于开发,一个用于生产。 So in your case something like:所以在你的情况下是这样的:

"scripts": {
   "start": "NODE_ENV=development BABEL_ENV=development node bootstrap.js",
   "serve": "NODE_ENV=production BABEL_ENV=production node bootstrap.js"
}

Now you can run your node app like this:现在您可以像这样运行您的节点应用程序:

In development开发中

node run start or node start (because node start is an alias for node run start) node run startnode start (因为 node start 是 node run start 的别名)

and in production并在生产中

node run serve (no shorthands here) node run serve (这里没有简写)

If you still think your package.json becomes too large, you can abstract that away to some .js files.如果您仍然认为您的 package.json 变得太大,您可以将其抽象为一些 .js 文件。 And change your scripts accordingly to something like:并相应地将您的脚本更改为:

"scripts": {
   "start": "node scripts/start.js"
   "serve": "node scripts/serve.js"
}

In those script files you can define both of those environment variables before running your app.在这些脚本文件中,您可以在运行应用程序之前定义这两个环境变量。

Error: Using babel-preset-react-app requires that you specify NODE_ENV or BABEL_ENV错误:使用 babel-preset-react-app 需要您指定 NODE_ENV 或 BABEL_ENV

Answer for CRA(create react app): CRA(创建反应应用程序)的答案:

@origin post https://github.com/facebook/create-react-app/issues/2377 @origin 帖子https://github.com/facebook/create-react-app/issues/2377

#Error: #错误:

0:0  error  Parsing error: [BABEL] {some-path}: Using `babel-preset-react-app` requires 
that you specify `NODE_ENV` or `BABEL_ENV` environment variables. Valid values are "development", "test", and "production". Instead, received: undefined. (While processing: "{project-path}\\node_modules\\babel-preset-react-app\\index.js")

#Description: #描述:

Problem occured after added .babelrc file to CRA using @rescripts/cli and @rescripts/rescript-use-babel-config .使用@rescripts/cli@rescripts/rescript-use-babel-config.babelrc文件添加到 CRA 后出现问题。

For fix error you should:要修复错误,您应该:

Typescript:打字稿:

Added "parser": "@typescript-eslint/parser", to your eslint.为您的 eslint 添加了"parser": "@typescript-eslint/parser"

Javascript: Javascript:

Added "parser": "@babel/eslint-parser", to your eslint.为你的 eslint 添加了"parser": "@babel/eslint-parser"

Eslint docs: https://eslint.org/docs/user-guide/configuring/plugins#specifying-parser Eslint 文档: https ://eslint.org/docs/user-guide/configuring/plugins#specifying-parser

Example of typescript eslint config in package.json for CRA: CRA 的 package.json 中的 typescript eslint 配置示例:

"eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest",
      "eslint:recommended"
    ],
    "globals": {
      "NodeJS": true
    },
    "parser": "@typescript-eslint/parser",
    "overrides": [
        {
            "files": ["*.ts", "*.tsx"],
            "rules": {
                "no-undef": "off",
                "no-unused-vars": "off",
                "@typescript-eslint/no-unused-vars": ["error"]
            }
        }
    ],
    "rules": {
      "no-empty": [
        "error",
        {
          "allowEmptyCatch": true
        }
      ]
    }
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 babel-preset-react-app 没有选择环境变量 - babel-preset-react-app not picking up environment variables 适用于Android的React Native-使用./gradlew assembleRelease将产生“找不到模块'babel-preset-react-app'”错误 - React Native for Android - Using ./gradlew assembleRelease will produce “Cannot find module 'babel-preset-react-app' “ error 出现“错误:插件/预设文件不允许导出对象,只能导出功能。” 来自 babel-preset-react-app/index.js - Getting "Error: Plugin/Preset files are not allowed to export objects, only functions." from babel-preset-react-app/index.js babel-preset-react-app/node_modules/@babel/runtime/helpers/esm/defineProperty.js:1 SyntaxError: createScript 意外的令牌导出 (vm.js:80:10 - babel-preset-react-app/node_modules/@babel/runtime/helpers/esm/defineProperty.js:1 SyntaxError: Unexpected token export at createScript (vm.js:80:10 您试图导入项目 src/ 目录之外的 /babel-preset-react-app/node_modules/@babel/runtime/helpers/esm/objectSpread2 - You attempted to import /babel-preset-react-app/node_modules/@babel/runtime/helpers/esm/objectSpread2 which falls outside of the project src/ dir 使用babel-preset-env进行转换,而无需从create-react-app弹出 - Transpile using babel-preset-env without ejecting from create-react-app 在 React 和 Webpack 项目上从“babel-preset-es2015”转换为“babel-preset-env”时出现问题 - Issue when transitioning from "babel-preset-es2015" to "babel-preset-env" on React and Webpack project 如何安装 babel-cli babel-preset-react - how to install babel-cli babel-preset-react 找不到预设“@ babel / env” - reactjs - Couldn't find preset “@babel/env” - reactjs 计算属性不适用于 Babel preset-env - Computed Properties not working with Babel preset-env
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM