qstash-js: v2: Module not found: Can't resolve '@upstash/qstash/nextjs'

npm ls @upstash/qstash:

└── @upstash/qstash@2.0.0

After the update, my application can’t find the qstash/nextjs path - when I click on it using vscode intellisense, it jumps to node_modules/@upstash/qstash/dist/nextjs.d.ts just fine. I am confused, any ideas why this may happen in a not too exotic nextjs setup?

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 30 (12 by maintainers)

Most upvoted comments

@bastienrobert yeah that was a mistake on my part I’ve reverted it in v2.1.2

import { verifySignature } from '@upstash/qstash/dist/nextjs' this still works, and I’ll figure out a way to make it nicer hopfully soon 😃

Okay. This issue will be fixed ASAP.

In my case, for both imports (with and without /dist in the import path) I’m getting the following error:

error Error [ERR_MODULE_NOT_FOUND]: Cannot find module '...\node_modules\next\server' imported from ...\node_modules\@upstash\qstash\nextjs.mjs
Did you mean to import next/server.js?

I’m using @upstash/qstash v2.5.2 (latest when writing this) and next v13.4.19.

For anyone looking for a solution As I just need the verifySignature method from the library, I have just copy-pasted the source code that can be found here: https://github.com/upstash/qstash-js/blob/main/src/nextjs.ts

The code:

import { Receiver } from "@upstash/qstash";
import { NextApiHandler, NextApiRequest, NextApiResponse } from "next";

type VerifySignatureConfig = {
  currentSigningKey?: string;
  nextSigningKey?: string;
  /**
   * The url of this api route, including the protocol.
   *
   * If you omit this, the url will be automatically determined by checking the `VERCEL_URL` env variable and assuming `https`
   */
  url?: string;
  /**
   * Number of seconds to tolerate when checking `nbf` and `exp` claims, to deal with small clock differences among different servers
   *
   * @default 0
   */
  clockTolerance?: number;
};

/**
 * TODO: This function is copy-pasted from: https://github.com/upstash/qstash-js/blob/main/src/nextjs.ts
 * This should be removed once import from the package is stable and published.
 * More info here: https://github.com/upstash/qstash-js/issues/44
 * @param handler 
 * @param config 
 * @returns 
 */
export function verifySignature(
  handler: NextApiHandler,
  config?: VerifySignatureConfig
): NextApiHandler {
  const currentSigningKey =
    config?.currentSigningKey ?? process.env.QSTASH_CURRENT_SIGNING_KEY;
  if (!currentSigningKey) {
    throw new Error(
      "currentSigningKey is required, either in the config or as env variable QSTASH_CURRENT_SIGNING_KEY"
    );
  }
  const nextSigningKey =
    config?.nextSigningKey ?? process.env.QSTASH_NEXT_SIGNING_KEY;
  if (!nextSigningKey) {
    throw new Error(
      "nextSigningKey is required, either in the config or as env variable QSTASH_NEXT_SIGNING_KEY"
    );
  }
  const receiver = new Receiver({
    currentSigningKey,
    nextSigningKey,
  });

  return async (request: NextApiRequest, response: NextApiResponse) => {
    // @ts-ignore This can throw errors during vercel build
    const signature = request.headers["upstash-signature"];
    if (!signature) {
      response.status(400);
      response.send("`Upstash-Signature` header is missing");
      response.end();
      return;
    }
    if (typeof signature !== "string") {
      throw new TypeError("`Upstash-Signature` header is not a string");
    }

    const chunks = [];
    for await (const chunk of request) {
      chunks.push(typeof chunk === "string" ? Buffer.from(chunk) : chunk);
    }
    const body = Buffer.concat(chunks).toString("utf8");

    const isValid = await receiver.verify({
      signature,
      body,
      clockTolerance: config?.clockTolerance,
    });
    if (!isValid) {
      response.status(400);
      response.send("Invalid signature");
      response.end();
      return;
    }

    try {
      request.body = (
        request.headers["content-type"] === "application/json"
          ? JSON.parse(body)
          : body
      ) as string;
    } catch {
      request.body = body;
    }

    return handler(request, response);
  };
}

Should be fixed here: https://github.com/upstash/qstash-js/releases/tag/v2.5.5. I personally tested both app and pages to make sure they are good. Lemme know if issue still persists.

Thank you, I can confirm this resolved my build issues

I can confirm I’m having the same issue when upgrading from 2.5.0 to 2.5.4 and a workaround of copying https://github.com/upstash/qstash-js/blob/main/src/nextjs.ts worked for me as well. I can’t share repro in repo as it’s not open sourced but I do have an error if that helps:

unhandledRejection Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/Users/username/myrepo/node_modules/.pnpm/node_modules/next/server' imported from /Users/username/myrepo/node_modules/.pnpm/@upstash+qstash@2.5.4/node_modules/@upstash/qstash/nextjs.mjs
Did you mean to import next@14.2.3_@babel+core@7.24.0_@opentelemetry+api@1.8.0_react-dom@18.3.1_react@18.3.1/node_modules/next/server.js?
    at new NodeError (node:internal/errors:405:5)
    at finalizeResolution (node:internal/modules/esm/resolve:327:11)
    at moduleResolve (node:internal/modules/esm/resolve:980:10)
    at defaultResolve (node:internal/modules/esm/resolve:1193:11)
    at ModuleLoader.defaultResolve (node:internal/modules/esm/loader:404:12)
    at ModuleLoader.resolve (node:internal/modules/esm/loader:373:25)
    at ModuleLoader.getModuleJob (node:internal/modules/esm/loader:250:38)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:76:39)
    at link (node:internal/modules/esm/module_job:75:36) {
  type: 'Error',
  url: 'file:///Users/username/myrepo/node_modules/.pnpm/node_modules/next/server',
  code: 'ERR_MODULE_NOT_FOUND'
}

@sitoftonic Weird. I’ll check the SDK and make sure it doesn’t require copy-paste. I tested edge and serverless everything was fine I actually deployed and check them myself. Maybe the pages directory causing this.

Also can add these to your tsconfig in my case they were causing the issue.

    "module": "esnext",
    "moduleResolution": "bundler"

Hi there, I’m having the same issue.

The workaround i’ve found is updating to v2.1.0 and changing the import to this

import { verifySignature } from '@upstash/qstash/dist/nextjs';

yeah this should work, but it’s ugly

Should be fixed here: https://github.com/upstash/qstash-js/releases/tag/v2.5.5. I personally tested both app and pages to make sure they are good. Lemme know if issue still persists.

Same issue here

Nextjs.verifySignature doesn’t seem to work.

Dang. I tried in a few setups and it worked. Can you try and reproduce it in a fresh project please?