Prod Deployment

@aaron-ford
@aaron-ford Adocasts Plus

I've been trying to get my code running on a 'prod' server. I think I am close. I'm getting an error linked to the use('web').user in the config/inertia.ts file that I'm not getting locally, and not sure why. There is no user logged in on the login page, but that's the same locally. Any ideas why it works locally but not on the server?

sharedData: {
    user: (ctx) => {
      const user = ctx.auth.use('web').user
      return user && new UserDto(user)
    },
Copied!
1
Create a free account to join in on the discussion
  1. @tomgobich

    Hey @aaron-ford! What's the error message you're getting?

    If it works fine locally but not in production, then the answer probably lies in a difference between those two environments, like maybe a different NodeJS version. Try installing the same version you're using in production on your local machine. Then build the project for production locally and attempt to run it to see if you're able to.

    0
    1. Responding to tomgobich
      @aaron-ford

      I will double check the node versions, but it seems ctx.auth is undefined on the login page, which makes sense since no one is logged in. But since that isn't an issue locally it seems odd. The app is running but the front end doesn't load. Here's the error:

      {"type":"TypeError","message":"Cannot read properties of undefined (reading 'use')","stack":"TypeError: Cannot read properties of undefined (reading 'use')

      1
      1. Responding to aaron-ford
        @tomgobich

        Yeah ctx.auth is coming through undefined when it should be the authenticator, even when a user isn't logged in. So, something is causing it to be misconfigured on production but not locally. A difference in Node version is one possibility. Double-check that the packages needed for authentication @adonisjs/auth, argon2, bcrypt, etc are all installed as dependencies and not dev dependencies, that's another possibility.

        Attempting to follow your deploy build pipeline locally should help pinpoint the issue, especially if you're able to replicate the issue locally because then you can step through it with the debugger or console logs to see what is having troubles.

        Oh - also make sure your NODE_ENV within your environment variables is set to production! I've seen that cause some weird issues before.

        0
        1. Responding to tomgobich

          I built and ran it locally and I get ERROR (7830): Failed to load url inertia/app/ssr.ts (resolved id: inertia/app/ssr.ts). Does the file exist?. The entry point for inertia is inertia/app/ssr.ts as defined in the vite.config.js and config/inertia.js files. But npm run build doesn't create an inertia folder, so I assume that is why it is failing? There is no inertia folder with app/ssr.ts. There is an app folder with ssr.ts in it. So when building and running from the build folder, is the entry point supposed to change somehow? Or is it building wrong and it should be putting an inertia folder in the build? After configuring the server, here are the steps I take to build and run the app:

          1. npm run build

          2. cp .env build/.env

          3. cd build

          4. npm ci —omit="dev"

          5. pm2 start build/bin/server.js --name myApp

          When I run node bin/server.js locally I also get this message: Could not auto-determine entry point from rollupOptions or html files and there are no explicit optimizeDeps.include patterns. Skipping dependency pre-bundling.

          1
          1. Responding to aaron-ford
            @tomgobich

            Just gave this a go locally on a new Inertia app and I get "Failed to load url inertia/app/ssr.ts" when I have NODE_ENV=development. The Inertia entrypoint is different between development and production. In production mode it'll be ./build/ssr/ssr.js. Setting NODE_ENV=production should fix that up so its looking in the correct spot for the build.

            I'm not getting the rollup options error though, but that sounds like it might just be because the SSR file couldn't be found and I have a hunch changing the NODE_ENV should fix that up as well. If, for any reason it doesn't, please share your vite.config.ts file! There might be a hint in there.

            0
New Discussion
Topic