简体   繁体   English

process.node.env 在 eslintrc.js 中未定义

[英]process.node.env is undefined in eslintrc.js

I need some linting rules to throw either an error or a warning depending on whether the build is development or production.我需要一些 linting 规则来根据构建是开发还是生产来抛出错误或警告。 In a React component file during development process.env.NODE_ENV === 'development' .在开发process.env.NODE_ENV的 React 组件文件中process.env.NODE_ENV === 'development'

In eslintrc.js I have:在 eslintrc.js 我有:

const production = process.env.NODE_ENV !== 'development'; // returns true
console.log('%c process.env.NODE_ENV', 'color: green;', process.env.NODE_ENV); // returns undefined

I want to be able to switch between linting rule warnings.我希望能够在 linting 规则警告之间切换。 and errors like this:和这样的错误:

    rules: {
        'no-tabs': 0,
        indent: [2, 'tab', { SwitchCase: 1, VariableDeclarator: 1 }],
        'react/jsx-props-no-spreading': 'off',
        'no-unused-vars':
            production
                ? 'error'
                : 'warn',

Why is process.env.NODE_ENV undefined and how can I fix this?为什么process.env.NODE_ENV未定义,我该如何解决?

You might think about just having multiple configuration files for eslint instead of trying to handle environment changes in a single file.您可能会考虑只为 eslint 设置多个配置文件,而不是尝试在单个文件中处理环境更改。

According to the docs: https://eslint.org/docs/user-guide/command-line-interface#basic-configuration根据文档: https : //eslint.org/docs/user-guide/command-line-interface#basic-configuration

You can just call eslint with the -c or --config flags and pass in the extra config file.您可以使用-c--config标志调用 eslint 并传入额外的配置文件。 This will merge a base config file with the file passed in the flag这会将基本配置文件与传入标志的文件合并

An example for two lint scripts:两个 lint 脚本的示例:
eslint --config ./dev-config.js **/*.js
eslint --config ./prod-config.js **/*.js

Since it's undefined during static analysis you can always do something like this:由于它在静态分析期间未定义,因此您始终可以执行以下操作:

  'no-console': (() => {
      if (typeof process.env.NODE_ENV === 'undefined') {
        return 'off';
      }

      if (process.env.NODE_ENV === 'development') {
        return 'off';
      }

      return 'error';
    })(),

暂无
暂无

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

相关问题 React Native 中的“.eslintrc.js”是什么? - What is “.eslintrc.js” in React Native? .eslintrc.js 用于 React 17 和 JSX,无需导入“react” - .eslintrc.js for React 17 and JSX without import 'react' 插件“react”在“.eslintrc.js”和“BaseConfig”之间发生冲突 - Plugin "react" was conflicted between ".eslintrc.js" and "BaseConfig 反应 | webpack | 在 Azure 上获取 process.env.NODE_ENV== undefined - React | webpack | getting process.env.NODE_ENV== undefined on Azure process.env.NODE_ENV 在一个项目中未定义,但在另一个项目中未定义 - process.env.NODE_ENV is undefined in one project, but not another 错误:无法加载在“.eslintrc.js”中声明的解析器“@babel/eslint-parser”:找不到模块“@babel/core/package.json” - Error: Failed to load parser '@babel/eslint-parser' declared in '.eslintrc.js': Cannot find module '@babel/core/package.json' process.env.NODE_ENV 在 webpack 4 中返回 undefined,服务器端 - process.env.NODE_ENV returns undefined in webpack 4, server-side 此node.js代码中的process.env.UNIVERSAL是什么 - What is process.env.UNIVERSAL in this node.js code 解析错误:已为 @typescript-eslint/parser 设置“parserOptions.project”。 该文件与您的项目配置不匹配:.eslintrc.js - Parsing error: "parserOptions.project" has been set for @typescript-eslint/parser. The file does not match your project config: .eslintrc.js 在已经使用和扩展“eslint-config-airbnb”的同时,我是否必须在 eslintrc.js 配置中使用和“扩展”eslint-plugin-react - Do I have to use and 'extend' eslint-plugin-react in eslintrc.js config while using and extending 'eslint-config-airbnb' already
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM