profile
viewpoint

Ask questionsCan’t import fs Promises API with ECMAScript Modules

  • Version: v10.2.1
  • Platform: Windows 10 64-bit
  • Subsystem: fs

Per the docs, “The API is accessible via require('fs').promises.”

When using ECMAScript Modules it can’t be accessed like that. One may expect to be able to load it with import {promises as fs} from 'fs', but this gives an error.

import {promises as fs} from 'fs'
        ^^^^^^^^
SyntaxError: The requested module 'fs' does not provide an export named 'promises'
    at ModuleJob._instantiate (internal/modules/esm/module_job.js:89:21)

So right now accessing the fs Promises API directly when using ECMAScript Modules requires two lines, like so:

import {default as fsWithCallbacks} from 'fs'
const fs = fsWithCallbacks.promises
nodejs/node

Answer questions michaelbrawn

const fsPromises = require('fs').promises; works for me.

Unfortunately, in TS, ES6, ESLint, await/async land with @typescript-eslint/no-require-imports enabled, this workaround isn't ideal.

Still waiting on something as simple as

import { readFile } from 'fs/promises'

or

import { readFile } from 'fs.promises'

or best yet,

import { readFileAsync } from 'fs'
useful!
source:https://uonfu.com/
answerer
Michael Brawn michaelbrawn Brawn & Brains IT Services Inc. Canada http://michaelbrawn.ca Ruby on Rails Software Developer
Github User Rank List