[英]process.node.env is undefined in eslintrc.js
我需要一些 linting 規則來根據構建是開發還是生產來拋出錯誤或警告。 在開發process.env.NODE_ENV
的 React 組件文件中process.env.NODE_ENV
=== 'development'
。
在 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
我希望能夠在 linting 規則警告之間切換。 和這樣的錯誤:
rules: {
'no-tabs': 0,
indent: [2, 'tab', { SwitchCase: 1, VariableDeclarator: 1 }],
'react/jsx-props-no-spreading': 'off',
'no-unused-vars':
production
? 'error'
: 'warn',
為什么process.env.NODE_ENV
未定義,我該如何解決?
您可能會考慮只為 eslint 設置多個配置文件,而不是嘗試在單個文件中處理環境更改。
根據文檔: https : //eslint.org/docs/user-guide/command-line-interface#basic-configuration
您可以使用-c
或--config
標志調用 eslint 並傳入額外的配置文件。 這會將基本配置文件與傳入標志的文件合並
兩個 lint 腳本的示例:
eslint --config ./dev-config.js **/*.js
eslint --config ./prod-config.js **/*.js
由於它在靜態分析期間未定義,因此您始終可以執行以下操作:
'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.