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

"Cannot find module" error when calling query within another query #2434

Closed
minajevs opened this issue Dec 30, 2024 · 1 comment
Closed

"Cannot find module" error when calling query within another query #2434

minajevs opened this issue Dec 30, 2024 · 1 comment

Comments

@minajevs
Copy link

Describe the bug
On a server, calling a query from another query throws an error Error [ERR_MODULE_NOT_FOUND]: Cannot find module ....

To Reproduce
Sample repo

  1. Create 2 very basic queries
query getFirstName {
  fn: import {getFirstName} from "@src/getFirstName",
  entities: []
}

query getFullName {
  fn: import {getFullName} from "@src/getFullName",
  entities: []
}
  1. Implement first query and import some external function into the query file:
import { type GetFirstName } from "wasp/server/operations";
import { capitalize } from "./capitalize"; // <-------- Important external import! 

export const getFirstName: GetFirstName<never, string> = () => {
  return capitalize("john");
};
  1. Implement second query and call first query from that
import { getFirstName, type GetFullName } from "wasp/server/operations";

export const getFullName: GetFullName<never, string> = async () => {
  const firstName = await getFirstName(); // <-------- Calling first query!
  return `${firstName} Smith`;
};
  1. Call a query from client
 const { data } = useQuery(getFullName);

Observed behavior
Server crashes with the following error:

[ Server!] Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/Users/***/dev/wasp-bug-sample/.wasp/out/sdk/wasp/dist/ext-src/capitalize' imported from /Users/***/dev/wasp-bug-sample/.wasp/out/sdk/wasp/dist/ext-src/getFirstName.js
[ Server!]     at finalizeResolution (node:internal/modules/esm/resolve:257:11)
[ Server!]     at moduleResolve (node:internal/modules/esm/resolve:913:10)
[ Server!]     at defaultResolve (node:internal/modules/esm/resolve:1037:11)
[ Server!]     at ModuleLoader.defaultResolve (node:internal/modules/esm/loader:650:12)
[ Server!]     at #cachedDefaultResolve (node:internal/modules/esm/loader:599:25)
[ Server!]     at ModuleLoader.resolve (node:internal/modules/esm/loader:582:38)
[ Server!]     at ModuleLoader.getModuleJobForImport (node:internal/modules/esm/loader:241:38)
[ Server!]     at ModuleJob._link (node:internal/modules/esm/module_job:132:49) {
[ Server!]   code: 'ERR_MODULE_NOT_FOUND',
[ Server!]   url: 'file:///Users/***/dev/wasp-bug-sample/.wasp/out/sdk/wasp/dist/ext-src/capitalize'

Removing external import (i.e. removing import { capitalize } from "./capitalize"; and inlining capitalize function) fixes the issue.

Environment

  • Apple Sillicon
  • Node.js v22.11.0
  • Wasp 15.0

Additional context

It is possible to "work around" the issue by commenting out problematic line, thefore causing build error, and then uncommenting the line again. I would speculate that there's a race condition during build time and external module (capitalize) is being built later than query (getFirstName)

@sodic
Copy link
Contributor

sodic commented Jan 8, 2025

Hey @minajevs, I finally found the time to dig into this.

The short version: Change ./capitalize to ./capitalize.js and everything should work. You must specify the js extension for relative imports in server-side code (the client-side code will tolerate both js and no extension).

The easiest way is always to use the .js extension.

The long version

Import extensions are one of Wasp DX's biggest pain points. Learn the details here if you're curious: #2222.

P.S. I just created a new issue (#2440) to cover this problem. We accidentally closed the one covering it (#1363 (comment)) because it also discussed a different problem (which we solved).

So, thanks for the report!
Moral of the story: Respect to the Single responsibility principle when writing issues :)

@infomiho infomiho closed this as completed Jan 8, 2025
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

3 participants