Server-only modules for Nitro
Hello all. I'm just starting to work with nuxt...I got a basic app running, and then added some modules in the server/ directory to interface with my existing SQL Server database in order to implement logins and sessions.
However, this seems to have broken nuxt.
I'm seeing in the browser devtools console that nuxt is trying to load all my server/lib/db files and choking on the node-mssql library. I thought everything in the server/ directory was server-only, but apparently not...
What is the preferred method for adding modules for use only in the server side api handlers so it won't try to load them on the client?
Thanks!
2 Replies
Are you not registering server routes in the sever folder (https://nuxt.com/docs/guide/directory-structure/server) are you trying to load something else? Can you give an example of what you have put in the server folder?
Nuxt
server/ · Nuxt Directory Structure
The server/ directory is used to register API and server handlers to your application.
Ok, I figured it out...
Just forgot how I initially set up the code on the client side of the login before I implemented the server side...
Turns out I was defining and exporting my
User
type (needed on client side) in the same module where I eventually defined the db functions (createUser
, getUserByUsername
, etc...)
So on the client side I was using:
but types.ts had:
and in users.ts:
...so the client now needed to load mssql 😱
tldr; Define types separately from db implementation