vite-fastify-boilerplate icon indicating copy to clipboard operation
vite-fastify-boilerplate copied to clipboard

How does it work with @fastify/autoload ?

Open ianzone opened this issue 3 years ago • 6 comments

Very cool boilerplate, but I got lost when trying to add @fastify/autoload...

ianzone avatar May 31 '22 01:05 ianzone

It works as usual, like described here https://github.com/fastify/fastify-autoload You should use ESM syntax:

// src/app.ts
import autoLoad from '@fastify/autoload'
import { dirname, join } from 'path'
import { fileURLToPath } from 'url'

export default async (opts?: FastifyServerOptions) => {
  const fastify = Fastify(opts)

  fastify.register(autoLoad, {
    dir: join(__dirname, 'plugins')
  })

...

ManUtopiK avatar May 31 '22 20:05 ManUtopiK

just found that the @fastify/autoload can not utilize the vite-plugin-node, it's reported here https://github.com/fastify/fastify-autoload/issues/230

and here is my test result:

❯ npm run dev

[email protected] dev vite

vite v2.9.9 dev server running at:

Local: http://localhost:3000/ Network: use --host to expose

ready in 270ms.

/home/ian/templates/fastify/node_modules/.pnpm/@[email protected]/node_modules/@fastify/autoload/index.js:222 throw new Error(@fastify/autoload cannot import plugin at '${file}'. To fix this error compile TypeScript to JavaScript or use 'ts-node' to run your app.) ^

Error: @fastify/autoload cannot import plugin at '/home/ian/templates/fastify/src/routes/root.ts'. To fix this error compile TypeScript to JavaScript or use 'ts-node' to run your app. at findPlugins (/home/ian/templates/fastify/node_modules/.pnpm/@[email protected]/node_modules/@fastify/autoload/index.js:222:15) at async autoload (/home/ian/templates/fastify/node_modules/.pnpm/@[email protected]/node_modules/@fastify/autoload/index.js:30:22)

Node.js v18.2.0

ianzone avatar May 31 '22 21:05 ianzone

Thanks for your feedback. I'll take a look...

ManUtopiK avatar Jun 01 '22 00:06 ManUtopiK

Also for me, same issue.

clabnet avatar Nov 18 '22 22:11 clabnet

Similar issue in vitest that has a solution https://github.com/fastify/fastify-autoload/issues/230#issuecomment-1448972001

I have no idea if there's a similar solution for vite-plugin-node

tompahoward avatar May 29 '23 06:05 tompahoward

Thanks for the link. I will take a try...

ManUtopiK avatar May 29 '23 21:05 ManUtopiK