【发布时间】:2021-06-05 06:42:38
【问题描述】:
我启动了一个 Next.js 应用并配置了 eslint。 我的开发依赖项列表是:
"devDependencies": {
"babel-eslint": "^10.1.0",
"eslint": "^7.21.0",
"eslint-config-airbnb": "^18.2.1",
"eslint-config-prettier": "^8.1.0",
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-jsx-a11y": "^6.4.1",
"eslint-plugin-prettier": "^3.3.1",
"eslint-plugin-react": "^7.22.0",
"eslint-plugin-react-hooks": "^4.2.0"
}
我的 .eslintrc 文件是:
{
"env": {
"browser": true,
"commonjs": true,
"es6": true,
"node": true
},
"parser": "babel-eslint",
"extends": [
"eslint:recommended",
"airbnb",
"airbnb/hooks",
"plugin:react/recommended",
"plugin:import/errors",
"plugin:import/warnings",
"plugin:jsx-a11y/recommended",
"prettier"
],
"globals": {
"Atomics": "readonly",
"SharedArrayBuffer": "readonly"
},
"parserOptions": {
"ecmaFeatures": {
"jsx": true
},
"ecmaVersion": 11,
"sourceType": "module"
},
"settings": {
"react": {
"version": "detect"
}
},
"plugins": [
"react",
"react-hooks"
],
"rules": {
"react/react-in-jsx-scope": "off",
"react/jsx-filename-extension": [
1,
{
"extensions": [
".js",
".jsx"
]
}
],
"react/display-name": 1,
"react/jsx-props-no-spreading": "off",
"react/prop-types": "off",
"jsx-a11y/anchor-is-valid": "off"
}
}
我的 Node 版本是最新的 15.11(我在 MacOS 上,如果重要的话)。
问题是我正在导入:
import fs from 'fs/promises';
而 eslint 抛出
Unable to resolve path to module 'fs/promises'.
否则,脚本可以工作,但我不明白是什么触发了错误。
有人可以帮忙吗?
yarn add -D eslint babel-eslint eslint-config-airbnb eslint eslint-plugin-jsx-a11y eslint-plugin-import eslint-plugin-react eslint-plugin-react-hooks eslint-config-prettier eslint-plugin-prettier
.eslintrc 文件:(如我的问题正文中所述)
file-example.js:
import path from 'path';
import fs from 'fs/promises'; // <= this throws "Unable to resolve path to module 'fs/promises'.
function HomePage(props) {
const { products } = props;
return (
<ul>
{products.map((product) => (
<li key={product.id}>{product.title}</li>
))}
</ul>
);
}
export async function getStaticProps() {
const filePath = path.join(process.cwd(), 'data', 'dummy-backend.json');
const jsonData = await fs.readFile(filePath);
const data = JSON.parse(jsonData);
return {
props: {
products: data.products,
},
};
}
export default HomePage;
【问题讨论】:
-
奇怪。如果您还没有,我会尝试重新安装
node_modules文件夹(如果它已经存在,请删除它)。或者,您可以尝试import { promises } from "fs";看看是否可行。在那张纸条上,我在节点v14.16.0上,它似乎正在使用这两种方法正确导入。 -
谢谢!是的,我尝试重新安装模块(使用 yarn 和 npm),但错误仍然存在。
import {promises} from 'fs'根本不起作用并破坏了应用程序,恐怕...... -
v.14.4 产生相同的错误。我刚刚用 mcve 更新了我的问题,希望对您有所帮助!
-
啊,在
getStaticProps中使用。看看这个 NextJS issue 和这个 NextJS discussion -
无法复制您的问题。此example repo 用作expected
标签: reactjs next.js eslint eslintrc