Skip to content

Ability to invoke @netlify/dev via CLI #406

Description

@benmccann

Right now use use netlify serve in the SvelteKit integration tests. We use it without any flags. It'd be nice if we could use @netlify/dev instead as it would let us drop hundreds of dependencies from netlify-cli. While I could theoretically wrap @netlify/dev in a CLI myself, it would introduce extra work for me and it'd be nicer if I could just call it directly

Activity

  1. added theissue type on Aug 19, 2025
  2. serhalp commented on Oct 10, 2025

    @serhalp
    Member

    Hey @benmccann I was taking a look at this again and this seems to work:

    import { NetlifyDev } from "@netlify/dev";
    
    const netlifyDev = new NetlifyDev({
      projectRoot: ".",
    });
    
    let serverReady = netlifyDev.start();
    console.log(
      `Environment loaded. Emulating features: ${netlifyDev.getEnabledFeatures().join(", ")}.`,
    );
    
    export default {
      async fetch(req: Request) {
        await serverReady;
        return netlifyDev.handle(req);
      },
    };

    Install @netlify/dev and run npx srvx. Seems to work with my limited testing!

    We're not quite sure this makes sense to expose and publish 🤔.

    If this works for you, let me know and we'll document it in the README or something!

  3. benmccann commented on Oct 10, 2025

    @benmccann
    Author

    Thank you so much for looking at this!

    Unfortunately it doesn't work for me. I get:

    Server Error
    TypeError: Cannot read properties of undefined (reading 'status')
        at Array. (file:///home/bmccann/src/kit/node_modules/.pnpm/[email protected]/node_modules/srvx/dist/log.mjs:14:51)
    

    It seems to be trying to log an error, but can't because res is undefined:

    const log = (_options = {}) => {
    	return async (req, next) => {
    		const start = performance.now();
    		const res = await next();
    		const duration = performance.now() - start;
    		const statusColor = statusColors[Math.floor(res.status / 100)] || "red";
    		console.log(`${Colors.gray(`[${new Date().toLocaleTimeString()}]`)} ${Colors.bold(req.method)} ${Colors.blue(req.url)} [${Colors[statusColor](res.status + "")}] ${Colors.gray(`(${duration.toFixed(2)}ms)`)}`);
    		return res;
    	};
    };
    

    I set the projectRoot to build, which contains _app, _headers, and _redirects and provided --port=8888 to srvx. I pushed a branch here in case you're able to take a look and see anything I might have done wrong: sveltejs/kit#14686

  4. serhalp commented on Oct 11, 2025

    @serhalp
    Member

    My bad — classic mistake: I made a small change without testing right before sharing the code snippet here and it introduced that issue 🤦🏼.

    Change the last line to:

        const res = await netlifyDev.handle(req);
        return res ?? new Response("Not Found", { status: 404 });
  5. benmccann commented on Oct 11, 2025

    @benmccann
    Author

    Thanks! It's a step in the right direction. Now the question is why is it getting a 404? On main if I visit http://localhost:8888/read I get a response stating "Hello! This file is read by read from $app/server." whereas I get a 404 on the branch from sveltejs/kit#14686 using @netlify/dev

  6. teemingc commented on Jan 15, 2026

    @teemingc

    I did some digging and and the reason this doesn't work is because there are fundamental differences between netlify serve and netlify dev. I'll have to find out the exact differences but my hunch is that the @netlify/dev package won't solve our issue.

    EDIT: looking at the source code more closely, they seem almost the same? I'll keep digging to figure out why serve works but dev doesn't

  7. teemingc commented on Jan 15, 2026

    @teemingc

    I've managed to get it working! One of the reasons it wasn't before was because the dev server wasn't picking up our manifest.json files in the build output. It was also looking in the wrong directory for the functions (we output to .netlify/edge-functions but it looks for netlify/edge-functions by default).
    See https://vscode.dev/github/netlify/primitives/blob/main/packages/dev/src/main.ts#L557-L559.

    I'm thinking we probably need to overhaul our Netlify adapter output so that it matches the newer standards from Netlify. Otherwise, the easy workaround for now is to just explicitly specify these in the netlify config.

    EDIT: created sveltejs/kit#15169

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

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions