Skip to content

nitro/vite in node preset should serve public assets from the server folder and not going up to the public directory #3868

Closed

Description

@ernestoalejo

Environment

nitro 3.0.1-alpha.1
node v22.14.0

Reproduction

  1. Empty vite project (https://vite.new)
  2. Install nitro: npm i nitro + add vite.config.ts with the plugin.
  3. npm run build

Example project:
https://stackblitz.com/edit/vitejs-vite-1gfcfxdy

Describe the bug

The Node preset generates two folders: .output/server and .output/public. The server folder appears to be self-contained, including its own node_modules and all the build files (in this example, there’s no node_modules directory because there are no dependencies):

❯ ls .output/public
assets/   vite.svg

❯ ls .output/server/
index.mjs     package.json  vite.svg

However, the server tries to read public files from outside that folder:

const assets = {
  "/vite.svg": {
    "type": "image/svg+xml",
    "etag": '"5d9-9/Odcje3kalF1Spc16j7Nl8xM2Y"',
    "mtime": "2025-12-12T12:34:55.909Z",
    "size": 1497,
    "path": "../public/vite.svg"
  },
  "/assets/index-CgYMNPpC.js": {
    "type": "text/javascript; charset=utf-8",
    "etag": '"a18-LiowwUS0J37fkDvNtFxd7Bs1Pik"',
    "mtime": "2025-12-12T12:34:56.009Z",
    "size": 2584,
    "path": "../public/assets/index-CgYMNPpC.js"
  },
  "/assets/index-XlyKSLnJ.css": {
    "type": "text/css; charset=utf-8",
    "etag": '"4b0-UhNyv0a4d7OTwpHmdOyfIisUpMQ"',
    "mtime": "2025-12-12T12:34:56.009Z",
    "size": 1200,
    "path": "../public/assets/index-XlyKSLnJ.css"
  }
};
function readAsset(id) {
  const serverDir = dirname(fileURLToPath(globalThis.__nitro_main__));
  return promises.readFile(resolve(serverDir, assets[id].path));
}

If I deploy only the server folder (inside a container or anywhere) the ../public folder will not be present. On the other hand if I deploy the entire .output folder the node_modules won’t work as expected.

At the moment, I’m manually duplicating the node_modules folder and loading up both folders, which doesn’t seem to be the intended approach.

COPY .output .output
COPY .output/server/node_modules node_modules

Additional context

No response

Logs

Activity

  1. added
    pending triage
    on Dec 12, 2025
  2. coderabbitai commented on Dec 12, 2025

    @coderabbitai
    Contributor

    🔗 Similar Issues

    Possible Duplicates

    Related Issues

    🔗 Related PRs

    #3798 - perf!: remove nodeModulesDirs [merged]
    #3817 - fix(vite): skip early dev handler for special prefixes [merged]
    #3836 - refactor: env runners [merged]
    #3849 - feat: lib chunks [merged]
    #3854 - refactor(vite): keep SSR bundle external dependencies [merged]

    👤 Suggested Assignees

    • pi0
    • hi-ogawa
    • alan-agius4
    • zhyc9de
    • bitbytebit1

    Enable issue planning

    To enable issue planning, add the following to your .coderabbit.yaml:

    issue_enrichment:
      planning:
        enabled: true

    You can then request a plan by commenting @coderabbitai plan on any issue.

    🧪 Issue enrichment is currently in early access.

    To disable automatic issue enrichment, add the following to your .coderabbit.yaml:

    issue_enrichment:
      auto_enrich:
        enabled: false
  3. pi0 commented on Dec 12, 2025

    @pi0
    Member

    You should upload or add .output to the docker container as output.

    Nitro node server, by default, serves static assets. You can disable behavior using serveStatic: false config if only want to include .output/server and serve .output/public via CDN/Proxy directly.

  4. ernestoalejo commented on Dec 13, 2025

    @ernestoalejo
    Author

    OK, I don't mind serving the assets duplicated from the CDN and having the option is awesome too. But there's something I don't quite understand: why are there libraries in .output/server/node_modules? Node doesn't seem to look for them in that location when I run node .output/server/index.mjs.

    I initially assumed it was a self-contained folder, but I guess that’s not the case.

    In any case, I'll go ahead and close the issue since it turned out not to be valid.

  5. pi0 commented on Dec 14, 2025

    @pi0
    Member

    .output is a self contained dir thats why Nitro traces dependencies into it.

    With node resolution algorithm, closest node_modules dir to importer will be searched first.

  6. ernestoalejo commented on Dec 14, 2025

    @ernestoalejo
    Author

    Thank you for the explanation. I looked deeper and discovered that the missing libraries error comes from the ESM module loader interceptor added by Sentry. The raw Node module loading behavior works fine on its own so that’s not really a nitro issue.

    Thanks for your time!

  7. sandros94 commented on Feb 5, 2026

    @sandros94
    Contributor

    As I was doing some experiments I noticed and got curious about the fact that nitro/vite does duplicate the content of the public folder into the server folder. What would be the advantage of doing so?

    // [email protected]
    >> ls .output/public
    assets nitro.svg preact.svg robots.txt vite.svg
    
    >> ls .output/server
    _libs _routes index.mjs nitro.svg preact.svg robots.txt vite.svg 
    

    I don't even see where the server one are getting imported/used and serveStatic only refers to those in the public dir right?
    If someone adds a video into the public folder this could easily end up creating triple digit artifact (ofc this doesn't make sense, why would you version control such file. But I've seen this happen multiple times out in the wild).

    I currently don't see the point of having them in .output/server, especially with serveStatic: false which I assume is one of the configs used when splitting deploments (public being served via CDN/Proxy)

  8. pi0 commented on Feb 5, 2026

    @pi0
    Member

    It is likely a vite misconfig issue that this happens. shouldn't happen with nitro cli / rolldown.

  9. sandros94 commented on Feb 6, 2026

    @sandros94
    Contributor

    @pi0 thanks and indeed I've found it: build.copyPublicDir

    I do think that this should be set to false since nitro handles them. If you want I could study and work on a PR, or would you like to approach this differently or yourself?

  10. pi0 commented on Feb 6, 2026

    @pi0
    Member

    Yes please feel free to make a PR! I think we should disable it for SSR and Nitro vite env configs (only client consumers)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    pending triage

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions