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

Getting "TypeError: __getAllUserFiles is not a function" when running production server #11

Closed
stephensamra opened this issue Mar 17, 2021 · 7 comments

Comments

@stephensamra
Copy link
Contributor

Hi there,

When I run yarn server:prod the server starts fine (I see the server running at http://localhost:3000 message) but, when I try to make a request from the browser I get an error message in the console and the browser never receives a response.

The dev server (yarn dev) does not have this problem.

I followed the "Manual Installation" section of the README for React + TypeScript so it's possible I missed something but, as far as I can tell, what I have is the same as the react-typescript boilerplate.

Error message:

(node:27112) UnhandledPromiseRejectionWarning: TypeError: __getAllUserFiles is not a function
    at Object.getAllUserFiles (.../node_modules/vite-plugin-ssr/user-files/infra.node.ts:13:10)
    at Object.getUserFiles (.../node_modules/vite-plugin-ssr/user-files/getUserFiles.shared.ts:18:38)
    at Object.getPageIds (.../node_modules/vite-plugin-ssr/route.node.ts:166:21)
    at renderPage (.../node_modules/vite-plugin-ssr/renderPage.node.ts:48:22)
    at .../index.ts:25:20

package.json script:

"scripts": {
    // ...
    "server:prod": "cross-env NODE_ENV=production ts-node ./server",
},

server/index.ts:

import express from 'express'
import { createPageRender } from 'vite-plugin-ssr'
import * as vite from 'vite'

const isProduction = process.env.NODE_ENV === 'production'
const root = `${__dirname}/..`

async function startServer() {
  const app = express()

  let viteDevServer: vite.ViteDevServer | undefined

  if (isProduction) {
    app.use(express.static(`${root}/dist/client`, { index: false }))
  } else {
    viteDevServer = await vite.createServer({ root, server: { middlewareMode: true }})
    app.use(viteDevServer.middlewares)
  }

  const renderPage = createPageRender({ viteDevServer, isProduction, root })

  app.get('*', async (req, res, next) => {
    const url = req.originalUrl
    const contextProps = {}
    const result = await renderPage({ url, contextProps })
    if (result.nothingRendered) return next()
    res.status(result.statusCode).send(result.renderResult)
  })

  const port = 3000
  app.listen(port, () => {
    console.log(`🚀 server running at http://localhost:${port}`)
  })
}

startServer()

Any help would be appreciated. Thanks in advance.

@brillout
Copy link
Member

What Vite version are you using?

@stephensamra
Copy link
Contributor Author

➜ vite -v
vite/2.1.0 darwin-x64 node-v14.3.0

@brillout
Copy link
Member

I can reproduce, let me fix this.

@stephensamra
Copy link
Contributor Author

Thank you!

@brillout
Copy link
Member

You need one more thing.

@brillout
Copy link
Member

There is aslo a bug vitejs/vite#2390 and a pending PR vitejs/vite#2519.

But I wrote a patch @brillout/vite-fix-2390 you can use until the PR is merged.

yarn add vite-plugin-ssr@0.1.0-beta.15
yarn add vite@2.1.2
yarn add @brillout/vite-fix-2390
yarn vite-fix-2390 # It should say `Vite already patched`
rm -r dist/ node_modules/.vite
yarn build
yarn server:prod

Let me know if you run into any issue.

@stephensamra
Copy link
Contributor Author

Works perfectly - thanks so much!

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

2 participants