Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use 'fs': Dynamic require of "fs" is not supported #38

Open
drewbitt opened this issue Jan 30, 2023 · 4 comments
Open

Use 'fs': Dynamic require of "fs" is not supported #38

drewbitt opened this issue Jan 30, 2023 · 4 comments

Comments

@drewbitt
Copy link

After building, running my Sveltekit project with deno run --allow-env --allow-read --allow-net build/index.js fails with

error: Uncaught Error: Dynamic require of "fs" is not supported
  throw new Error('Dynamic require of "' + x2 + '" is not supported');

I use a single readdir command in my Sveltekit backend

import { readdir } from 'fs/promises';
...
const files = await readdir(...);

To my deps.ts file I have added

export { readdir } from "https://deno.land/[email protected]/node/fs/promises.ts";
// export { fs } from "node:fs";
// export { readdir } from "node:fs/promises";

export { exists } from 'https://deno.land/[email protected]/fs/mod.ts';
export { dirname, extname, fromFileUrl, join } from "https://deno.land/[email protected]/path/mod.ts";
export { Application } from 'https://deno.land/x/[email protected]/mod.ts';

The readdir import does not remove the error and the built-in fs imports do not work

X [ERROR] Could not resolve "node:fs"

    .svelte-kit/deno/deps.ts:3:19:
      3 │ export { fs } from "node:fs";
        ╵                    ~~~~~~~~~

  The package "node:fs" wasn't found on the file system but is built into node. Are you trying to
  bundle for node? You can use "platform: 'node'" to do that, which will remove this error.

Are 'fs' and other built-in node modules expected to work?

@drewbitt
Copy link
Author

The project was switched to rollup while I was writing the issue, but I cannot test it as build/ is now separated into build/client and build/server while deno run -A build/server/index.js doesn't start the server.

@jpaquim
Copy link
Contributor

jpaquim commented Feb 1, 2023

@drewbitt thanks for your feedback, can you test if running with deno run -A build/index.js solves your issue? build/server/index.js only exports the Server but doesn't actually instantiate it.

@drewbitt
Copy link
Author

drewbitt commented Feb 2, 2023

Sorry I had something messed up with the install as I was importing from Github as 0.9 was not published to npm yet. The github version didn't give me a build/index.js but 0.9 does.

Slightly different error:

error: Uncaught TypeError: Relative import path "fs" not prefixed with / or ./ or ../
If you want to use a built-in Node module, add a "node:" prefix (ex. "node:fs").
    at file:///C:/Users/andys-pc/project/build/server/chunks/hooks.server-dbf7bfa9.js:4:8
      const module = await get_hooks();
                     ^
    at async Server.init (file:///C:/Users/andys-pc/project/build/server/index.js:3288:22)
    at async file:///C:/Users/andys-pc/project/build/handler.js:7:1

hooks.server chunk is just import 'fs'; and I've tried various export * as fs from 'node:fs/promises', export * as fs from 'node:fs';, etc

@leo-mangold
Copy link

Are there any fixes for this yet? I also ran into the same problem while running deno compile and running deno run I get the following errors:

TypeError: Relative import path "buffer" not prefixed with / or ./ or ../
If you want to use a built-in Node module, add a "node:" prefix (ex. "node:buffer").
    at file:///Users/leo/dev/projects/mangold-international/echo/build/server/chunks/_layout.svelte-898d49a1.js:16:8
    at async Module.component (file:///Users/leo/dev/projects/mangold-international/echo/build/server/chunks/0-f8f776b5.js:43:32)
    at async Promise.all (index 0)
    at async render_response (file:///Users/leo/dev/projects/mangold-international/echo/build/server/index.js:2424:21)
    at async render_page (file:///Users/leo/dev/projects/mangold-international/echo/build/server/index.js:3254:12)
    at async resolve (file:///Users/leo/dev/projects/mangold-international/echo/build/server/index.js:3789:22)
    at async respond (file:///Users/leo/dev/projects/mangold-international/echo/build/server/index.js:3680:22)
    at async ssr (file:///Users/leo/dev/projects/mangold-international/echo/build/handler.js:34:19)
    at async handler (file:///Users/leo/dev/projects/mangold-international/echo/build/handler.js:80:11)
    at async dispatch (https://deno.land/x/[email protected]/middleware.ts:41:7)
    at async Application.#handleRequest (https://deno.land/x/[email protected]/application.ts:436:9)
TypeError: Relative import path "buffer" not prefixed with / or ./ or ../
If you want to use a built-in Node module, add a "node:" prefix (ex. "node:buffer").
    at file:///Users/leo/dev/projects/mangold-international/echo/build/server/chunks/_layout.svelte-898d49a1.js:16:8
    at async Module.component (file:///Users/leo/dev/projects/mangold-international/echo/build/server/chunks/0-f8f776b5.js:43:32)
    at async Promise.all (index 0)
    at async render_response (file:///Users/leo/dev/projects/mangold-international/echo/build/server/index.js:2424:21)
    at async respond_with_error (file:///Users/leo/dev/projects/mangold-international/echo/build/server/index.js:2807:12)
    at async render_page (file:///Users/leo/dev/projects/mangold-international/echo/build/server/index.js:3271:12)
    at async resolve (file:///Users/leo/dev/projects/mangold-international/echo/build/server/index.js:3789:22)
    at async respond (file:///Users/leo/dev/projects/mangold-international/echo/build/server/index.js:3680:22)
    at async ssr (file:///Users/leo/dev/projects/mangold-international/echo/build/handler.js:34:19)
    at async handler (file:///Users/leo/dev/projects/mangold-international/echo/build/handler.js:80:11)
    at async dispatch (https://deno.land/x/[email protected]/middleware.ts:41:7)
layout load en
Error: Not found: /favicon.ico
    at resolve (file:///Users/leo/dev/projects/mangold-international/echo/build/server/index.js:3807:18)
    at resolve (file:///Users/leo/dev/projects/mangold-international/echo/build/server/index.js:3682:34)
    at Object.handle (file:///Users/leo/dev/projects/mangold-international/echo/build/server/chunks/hooks.server-78a6600e.js:15:10)
    at respond (file:///Users/leo/dev/projects/mangold-international/echo/build/server/index.js:3680:43)
    at eventLoopTick (ext:core/01_core.js:181:11)
TypeError: Relative import path "buffer" not prefixed with / or ./ or ../
If you want to use a built-in Node module, add a "node:" prefix (ex. "node:buffer").
    at file:///Users/leo/dev/projects/mangold-international/echo/build/server/chunks/_layout.svelte-898d49a1.js:16:8
    at async Module.component (file:///Users/leo/dev/projects/mangold-international/echo/build/server/chunks/0-f8f776b5.js:43:32)
    at async Promise.all (index 0)
    at async render_response (file:///Users/leo/dev/projects/mangold-international/echo/build/server/index.js:2424:21)
    at async respond_with_error (file:///Users/leo/dev/projects/mangold-international/echo/build/server/index.js:2807:12)
    at async resolve (file:///Users/leo/dev/projects/mangold-international/echo/build/server/index.js:3801:16)
    at async respond (file:///Users/leo/dev/projects/mangold-international/echo/build/server/index.js:3680:22)
    at async ssr (file:///Users/leo/dev/projects/mangold-international/echo/build/handler.js:34:19)
    at async handler (file:///Users/leo/dev/projects/mangold-international/echo/build/handler.js:80:11)
    at async dispatch (https://deno.land/x/[email protected]/middleware.ts:41:7)
    at async Application.#handleRequest (https://deno.land/x/[email protected]/application.ts:436:9)

Any suggestions on how to fix this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants