Error: Cannot find module '../../package.json'
Hi! Sorry, it's me again.
Problem
The build step runs fine, but now I'm seeing a new runtime error:
Error: Cannot find module '../../package.json'
Require stack:
- /vercel/path0/node_modules/steamapi/dist/src/SteamAPI.js
at Module._resolveFilename (node:internal/modules/cjs/loader:1145:15)
at /var/task/node_modules/next/dist/compiled/next-server/server.runtime.prod.js:11:28979
at Module._load (node:internal/modules/cjs/loader:986:27)
at /opt/rust/nodejs.js:1:11508
at Function.Wt (/opt/rust/nodejs.js:1:11878)
at Q.e.<computed>.K._load (/opt/rust/nodejs.js:1:11478)
at Module.require (node:internal/modules/cjs/loader:1233:19)
at u.require (/var/task/node_modules/next/dist/compiled/next-server/server.runtime.prod.js:11:29195)
at require (node:internal/modules/helpers:179:18)
at 8414 (/var/task/.next/server/app/api/trpc/[trpc]/route.js:8:41752) {
code: 'MODULE_NOT_FOUND',
requireStack: [ '/vercel/path0/node_modules/steamapi/dist/src/SteamAPI.js' ],
page: '/api/trpc/steam.getGameDetails'
}
This only happens on Vercel, not locally. Any clue what might be causing this?
Context
I'm using SteamAPI inside tRPC pretty normally:
import { z } from "zod";
import { createTRPCRouter, publicProcedure } from "~/server/api/trpc";
import { env } from "~/env";
import SteamAPI from "steamapi";
export const steamRouter = createTRPCRouter({
getGameDetails: publicProcedure
.input(z.object({ appId: z.number() }))
.query(async ({ input }) => {
const steam = new SteamAPI(env.STEAM_API_KEY);
return await steam.getGameDetails(input.appId);
}),
});
The above error occurs when I try to call the getGameDetails endpoint, which is under /api/trpc/steam.getGameDetails.
Thanks again for your help and speedy responses :)
Hmm I'm not familiar with Vercel so I'm not sure how that environment would affect imports. Would it be possible for you to join the Discord server and we can chat or you can screen share so I can try to debug? https://discord.gg/z9thk2jX
@twbrianho I don't know if this error is still an issue. Does this post on Stack Overflow help?
I think that may be a different issue. My build finishes successfully, this error happens at runtime on the server side.
I was struggling with this for a few hours today and yesterday, trying to get this library working with one of my projects. I don't use vercel, however, I think the deployment is similar enough that this may help you / potentially someone may know the fix on the steam api side, as I assume it has something to do with the bundling and internal file structure expectations. I am also using NextJS and pnpm, which could've been part of the problem too (unsure if you were). I added an explicit reference to steam api in my next config, which seemed to clear this up.
experimental: { outputFileTracingIncludes: { '/**': [ './node_modules/.pnpm/[email protected]/**/*', './node_modules/steamapi/**/*' ] } }
Is what I added to my next.config.ts
I also needed to add a bunch of type declarations for the linter to get off my ass, which you may need, depending on your use case. I can include them if you'd like, but the explicit file tracing declaration of the module is the main thing for me.
Hopefully this helps, as it did for me :)
Aw thanks! Appreciate you posting this!
I gave up and moved to a different solution altogether a while back, but I hope your comment helps others with the same problem in the future :D
serverExternalPackages: ["steamapi"], solved issue for me.
Is there anything that we can do to the library to prevent this from happening with Vercel?