[英]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.