pluvial / svelte-adapter-deno

A SvelteKit adapter for Deno
https://svelte-adapter-deno.deno.dev/
MIT License
313 stars 14 forks source link

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

Open drewbitt opened 1 year ago

drewbitt commented 1 year ago

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/std@0.175.0/node/fs/promises.ts";
// export { fs } from "node:fs";
// export { readdir } from "node:fs/promises";

export { exists } from 'https://deno.land/std@0.175.0/fs/mod.ts';
export { dirname, extname, fromFileUrl, join } from "https://deno.land/std@0.175.0/path/mod.ts";
export { Application } from 'https://deno.land/x/oak@v11.1.0/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 commented 1 year ago

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 commented 1 year ago

@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 commented 1 year ago

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 commented 1 year ago

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/oak@v11.1.0/middleware.ts:41:7)
    at async Application.#handleRequest (https://deno.land/x/oak@v11.1.0/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/oak@v11.1.0/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/oak@v11.1.0/middleware.ts:41:7)
    at async Application.#handleRequest (https://deno.land/x/oak@v11.1.0/application.ts:436:9)

Any suggestions on how to fix this?