question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`import {readFile} from 'fs/promises';` ESM syntax fails trying to import from a file

See original GitHub issue

Describe the bug A page component that imports promisified fs methods like this

<script context="module">
    import {readFile} from 'fs/promises';
</script>

…fails with the error Failed to resolve import "fs/promises" from "index.svelte". Does the file exist?:

Stacktrace
500
Failed to resolve import "fs/promises" from "index.svelte". Does the file exist?

Error: Failed to resolve import "fs/promises" from "index.svelte". Does the file exist?
    at formatError (/app/server/node_modules/vite/dist/node/chunks/dep-e9a16784.js:44070:46)
    at TransformContext.error (/app/server/node_modules/vite/dist/node/chunks/dep-e9a16784.js:44066:19)
    at normalizeUrl (/app/server/node_modules/vite/dist/node/chunks/dep-e9a16784.js:68952:26)
    at async TransformContext.transform (/app/server/node_modules/vite/dist/node/chunks/dep-e9a16784.js:69080:57)
    at async Object.transform (/app/server/node_modules/vite/dist/node/chunks/dep-e9a16784.js:44268:30)
    at async transformRequest (/app/server/node_modules/vite/dist/node/chunks/dep-e9a16784.js:58540:29)
    at async instantiateModule (/app/server/node_modules/vite/dist/node/chunks/dep-e9a16784.js:68145:10)

Information about your SvelteKit Installation:

Diagnostics
  • The output of npx envinfo --system --npmPackages svelte,@sveltejs/kit,@sveltejs/adapter-node,@sveltejs/adapter-static,@sveltejs/adapter-begin,@sveltejs/adapter-netlify,@sveltejs/adapter-vercel vite --binaries --browsers
  System:
    OS: Linux 5.10 Alpine Linux
    CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 2.36 GB / 7.77 GB
    Container: Yes
    Shell: 1.31.1 - /bin/ash
  Binaries:
    Node: 14.17.0 - /usr/local/bin/node
    Yarn: 1.22.5 - /usr/local/bin/yarn
    npm: 6.14.13 - /usr/local/bin/npm
  npmPackages:
    @sveltejs/adapter-node: next => 1.0.0-next.24
    @sveltejs/kit: ^1.0.0-next.115 => 1.0.0-next.115
    svelte: ^3.38.2 => 3.38.2

Severity I can work around this like so:

<script context="module">
    import {promises as fsPromises} from 'fs';
    const {readFile} = fsPromises;
</script>

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:6
  • Comments:10 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
mdynnlcommented, Jun 18, 2021

A repro-repo is probably not necessary. Just a fresh svelte-kit + node v16.3.0 and an endpoint with import fs from 'fs/promises'; console.log(fs) is enough. But for the sake of repro. Here’s the repro-repo => https://github.com/mdynnl/svelte-kit-issue-1689

1reaction
raythurnevoidcommented, Jul 23, 2021

I’m quite sure this is an issue caused by the fact that Vite uses this package to determine built-in node modules: https://github.com/sindresorhus/builtin-modules I’ve opened two issues about this: https://github.com/sindresorhus/builtin-modules/issues/12, https://github.com/vitejs/vite/issues/4037

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using import fs from 'fs' - javascript - Stack Overflow
For default exports you should use: import * as fs from 'fs';. Or in case the module has named exports: import {fs} from...
Read more >
Avoid these issues when using new ECMAScript modules in ...
I'll describe the typescript method. Most typescript applications importing commonjs modules should turn on esModuleInterop in their tsconfig ...
Read more >
ECMAScript modules | Node.js v19.3.0 Documentation
For example, attempting to load ./foo from data:text/javascript,import "./foo"; fails to resolve because there is no concept of relative resolution for data: ...
Read more >
fs-extra - npm
fs-extra contains methods that aren't included in the vanilla Node.js fs package. Such as recursive mkdir, copy, and remove.
Read more >
Documentation - ECMAScript Modules in Node.js - TypeScript
ts file is compiled as an ES module, ECMAScript import / export syntax is left alone in the .js output; when it's compiled...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found