Next.js + eslint: unable to resolve path to module 'fs/promises'

Viewed 1894

I started a Next.js app and configured eslint. My list of dev dependencies for it are:

"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"
}

And my .eslintrc file is:

{
    "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"
    }
}

My Node version is the latest, 15.11 (I'm on MacOS, if it matters).

The issue is that I'm importing:

import fs from 'fs/promises';

And eslint throws

Unable to resolve path to module 'fs/promises'.

Otherwise, the script works, but I don't understand what's triggering the error.

Could anyone help?

MCVE

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: (as above in the body of my question)

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;
1 Answers

Though I was not using react, but pure typescript, I experienced an same issue. I resolved it by configuring like the example below, with eslint-import-resolver-typescript.

  settings: {
    'import/resolver': {
      typescript: {
        alwaysTryTypes: true
      },
    },
  }

alwaysTryTypes: true made it works, as it lets eslint respect node_modules/@types/*, which includes @types/node, which contains "fs/promises" declaration (in case of recent version).

Related