0

Test repo at https://github.com/user72356/vite-project

I'm trying to import modules using their absolute path from within the api directory of my app. I'm able to use absolute paths in the front-end portion of the code, for example import hello from "shared/hello";, but not within the api directory.

Doing so results in a Cannot find module error. See https://github.com/user72356/vite-project/blob/main/api/index.ts to see the actual code.

I have limited experience with Vercel and Node.js but I think I may understand what's going on. Since anything in the api directory runs on Node, I suppose shared/hello doesn't map to anything on the filesystem. It only properly maps to something on the web, i.e. example.com/shared/hello. Let me know if I got this right.

How can I make shared/hello be a valid import path on the front-end as well as on the back-end (api folder). Is this even possible?

Konrad
  • 21,590
  • 4
  • 28
  • 64
sw1337
  • 533
  • 4
  • 16
  • Import statements without path prefixes are reserved for node modules. You can try `"/shared/hello"` – Konrad Nov 05 '22 at 22:25
  • Does this answer your question? [Absolute path not working in Vite project React TS](https://stackoverflow.com/questions/68241263/absolute-path-not-working-in-vite-project-react-ts) – Konrad Nov 05 '22 at 22:30
  • 1
    @KonradLinkowski The linked question doesn't apply, it's for the React side of the app, where I have no problem doing such imports. The api folder is for serverless functions and runs in a node environment (not the browser). I can't add a / prefix, it doesn't compile. – sw1337 Nov 05 '22 at 23:02

0 Answers0