node-fs-extra: Cannot find module 'fs-extra/esm' or its corresponding type declarations.ts(2307)

Both importing from fs-extra and fs-extra/esm doesn’t work anymore in v11.0.0. I tried the following lines in my fs.ts file:

import { ensureDirSync, ensureFileSync, removeSync } from 'fs-extra'
import { ensureDirSync, ensureFileSync, removeSync } from 'fs-extra/esm'

The top line does work in v10.1.0. Am I doing something wrong?

  • Operating System: Ubuntu 22.04.1 LTS
  • Node.js version: v16.18.0
  • fs-extra version: v11.0.0

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 16 (7 by maintainers)

Most upvoted comments

@RyanZim I just had a quick skim through https://www.sensedeep.com/blog/posts/2021/how-to-create-single-source-npm-module.html and found this:

One more wrinkle, TypeScript does not (yet) behave with exports. So you need to include the legacy module and main properties for TypeScript. The main property points to the CJS entry point and the module property points to the ESM entry.

Based on that, this should work:

{
  "main": "./lib/index.js",
  "module": "./lib/esm.mjs",
  "exports": {
    "import": "./lib/esm.mjs",
    "require": "./lib/index.js"
  }
}

In other words, removing main from package.json may have removed TypeScript support.

Update: Just double checked, and those changes resolve the issue with TypeScript.

(Background: https://github.com/mdn/yari/pull/7697)

FWIW I only found two packages among a long list of dependencies that we’re using that export as ./esm (flatted and ts-node), and both also define main and module in their package.json.

PS: The polite way would have been to add a review to my PR and ask me to revert the changes to the exports field. Just saying.