thgh / vercel-sapper

Vercel builder for Sapper with SSR enabled
MIT License
189 stars 25 forks source link

Turns out we couldn't find your server instance. Did you write `module.exports = app`? #63

Closed msyyn closed 3 years ago

msyyn commented 3 years ago

Hey, I am receiving the following error and couldn't get around what is causing it..

My deploy works just fine and finishes with no errors whatsoever, but when I load the site on Vercel it shows this in place of my site:


typeof: function (expected 'function')
String: (req, res) => {
      res.writeHead(500, { 'Content-Type': 'text/plain' })
      res.write('This is vercel-sapper, your Vercel builder. Turns out we couldn\'t find your server instance. Did you write "module.exports = app"?

typeof: ${typeof listener} (expected 'function')
String: ${String(listener)}

Read the docs or create an issue: https://github.com/thgh/vercel-sapper')
      res.end()
    }

Read the docs or create an issue: https://github.com/thgh/vercel-sapper

I tried doing what the error says; exporting it with module.exports too. My other project is using similar server file and it's working just fine?

This is my server.js:


import express from 'express';
import compression from 'compression';
import * as sapper from '@sapper/server';

import cookieParser from 'cookie-parser';
import { json } from 'body-parser';
import supabase from './supabase';

const { PORT, NODE_ENV } = process.env;
const dev = NODE_ENV === 'development';

export default express()
  .use(cookieParser())
  .use(json())
  .use(
    compression({ threshold: 0 }),
    sirv('static', { dev }),
    sapper.middleware({
      session: async (req, res) => {
        // If there's a JWT, check if it's valid
        const results = await supabase.auth.api.getUser(req.cookies['supaToken']);
        if (results.user) {
          return { userToken: req.cookies['supaToken'] };
        }
        return { userToken: false };
      }
    })
  )
  .listen(PORT, err => {
    if (err) console.log('error', err);
  });```
thgh commented 3 years ago

Sounds related to https://github.com/thgh/vercel-sapper/issues/54#issuecomment-741022512

nmcapule commented 3 years ago

I'm encountering the same issue. I did the change from #54 (comment) but the issue still persists.

Here's my server.ts

import sirv from "sirv";
import express from "express";
import compression from "compression";
import * as sapper from "@sapper/server";

const {
  PORT = 3000,
  NODE_ENV = "development",
} = process.env;
const dev = NODE_ENV === "development";

export default express()
  .use(
    compression({ threshold: 0 }),
    sirv("static", { dev }),
    sapper.middleware()
  )
  .listen(PORT);

Looks like this issue only shows up when using express. Changing express to polka fixes the problem on my Vercel deployment.

thgh commented 3 years ago

Try change export default into module.exports = At some point the Sapper template broke the default export behaviour, but module.exports always worked.

We can dive deeper on this if you can provide a test repo 😉

msyyn commented 3 years ago

Module exports doesn't seem to help either. Experiencing the issue with this at least: https://github.com/msyyn/sapper-discord-supabase-tailwind

msyyn commented 3 years ago

Okay so module.exports works.. I just had to do it like this:

const app = (module.exports = express())

and it goes through. :D

had it as module.exports = app before