Skip to content

Are the Getting Started docs updated?聽#1078

Open

Description

@JohnMikko27

馃挰 Are the Getting Started docs updated?

I'm in the Getting Started section of the fastify docs: https://fastify.dev/docs/latest/Guides/Getting-Started/ in the "Your first server" section. I'm just copy pasting the code provided to get our first server running, here's my code:

// this version does not work
const fastify = require('fastify')({
  logger: true
})

// Declare a route
fastify.get('/', function (request, reply) {
  reply.send({ hello: 'world' })
})

// Run the server!
fastify.listen({ port: 3000 }, function (err, address) {
  if (err) {
    fastify.log.error(err)
    process.exit(1)
  }
  console.log(`Server is now listening on ${address}`)
})

// // this version does work
// module.exports = async (fastify, opts) => {
//   fastify.get("/", (req, reply) => {
//     reply.send('hi module.exports')
//   })
// }

I get this error when trying the CommonJS, ES6, and async await versions that were listed in the docs. I'm new to Fastify so I'm unsure what this error means.
Image

But, I was able to find a working version, I was just curious why the ones listed in the docs weren't working for me.

Your Environment

  • node version: 20
  • fastify version: >=5.0.0
  • os: Mac

Activity

  1. metcoder95 commented on Jan 28, 2025

    @metcoder95
    Member

    Can you provide an Minimum Reproducible Example to support you better?

    So far, from your error code; you are trying to use the fastify CLI, which expects that the exported item from your script is a method that matches the plugin interface

  2. JohnMikko27 commented on Jan 28, 2025

    @JohnMikko27
    Author

    Can you provide an Minimum Reproducible Example to support you better?

    So far, from your error code; you are trying to use the fastify CLI, which expects that the exported item from your script is a method that matches the plugin interface

    Hi, thanks for reaching back.

    I'm not exactly sure how I can better improve my minimum reproducible example as I already copy-pasted my entire file (which isn't that long). Basically, I was just following along to the Getting Started guide in the docs. I ran:

    npm install fastify

    then copy pasted the code they provided. But I get an error when I run that code. If I comment the currently uncommented code and uncomment the code under the comment "this version does work" then I can successfully get the server running and get an output. Which is why I was wondering if the docs were updated.

    Let me know how I can improve my minimum reproducible example better and if you have any more questions. Thank you.

  3. dancastillo commented on Feb 2, 2025

    @dancastillo
    Member

    Looks like you are using fastify-cli to run your code. You can run the code in the snippet above using command node [file-name].js

    This section will tell you how to run using fastify-cli or you can check out the repo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wantedExtra attention is needed

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions