jprichardson / node-fs-extra

Node.js: extra methods for the fs object like copy(), remove(), mkdirs()
MIT License
9.43k stars 775 forks source link

Can't import json methods using default import name in ESM #1018

Closed 06000208 closed 9 months ago

06000208 commented 11 months ago

Per the readme, ESM imports are supported for fs-extra's own methods, like so:

import { outputFile, outputFileSync } from 'fs-extra/esm'

However, fs-extra's types and package configuration causes vscode to import the package like so when completing intellisense:

import { outputFile, outputFileSync } from "fs-extra";

That usually works fine, except the methods that deal with json: outputJson, readJson, writeJson, which result in a Named export not found error:

import { outputJson } from "fs-extra";
node test
file:///G:/root/projects/temp/test.js:1
import { outputJson } from "fs-extra";
         ^^^^^^^^^^
SyntaxError: Named export 'outputJson' not found. The requested module 'fs-extra' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from 'fs-extra';
const { outputJson } = pkg;

    at ModuleJob._instantiate (node:internal/modules/esm/module_job:124:21)
    at async ModuleJob.run (node:internal/modules/esm/module_job:190:5)

Node.js v18.13.0

Importing them using fs-extra/esm works...

import { outputJson, readJson, writeJson } from "fs-extra/esm";

But vscode intellisense won't import them that way, and they're missing all types:

2023-09-30-Code-1696127522

Importing them like so works with fs-extra's types:

import fse from "fs-extra";
const { outputJson } = fse;
console.log(outputJson);

But intellisense will still auto import them incorrectly if you write one that isn't already imported:

2023-09-30-Code-1696127763

RyanZim commented 11 months ago

Seems like there's a bug in the types here. fs-extra does not export types, they come from https://github.com/DefinitelyTyped/DefinitelyTyped, so the fix would need to be implemented there.