fastify/help

typescript fastifyPlugin issue when importing and running

Jzuni97 opened this issue · 3 comments

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the issue has not already been raised

Issue

Hi all,

This might not be a fastify related issue but more setup/integration with typescript.

I am trying to run the mongo plugin using the @fastify/mongodb but i run into the following issue when running the server:

src/api/plugins/index.ts:2:58 This condition will always return true since this function is always defined. Did you mean to call it instead?
2 var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {

Not really sure what is causing the error based on import and usage.
Any help would be appreciated.

mongodb plugin code

import fastifyPlugin from "fastify-plugin";
import { fastifyMongodb } from "@fastify/mongodb";

export const mongoPlugin = fastifyPlugin(async (fy, opts) => {
  if (!process.env.MONGO_CONNECTION_URI)
    throw new Error(
      `Mongo connection string is not present. Check env file for 'MONGO_CONNECTION_URI'`
    );
  fy.register(fastifyMongodb, {
    url: process.env.MONGO_CONNECTION_URI,
    forceClose: true,
  });
});

app.ts

import Fastify from "fastify";
import cors from "@fastify/cors";
import dotenv from "dotenv";
import { mongoPlugin} from "@apiv1/plugins";

dotenv.config(); // Load environment variables from .env file

const server = Fastify();

server.register(cors);
server.register(mongoPlugin);

server.get("/", async (request, reply) => {
  return { hello: "world" };
});

server.listen({ port: 3000 }, (err, address) => {
  if (err) {
    server.log.error(`Error initializing Based Server: ${err}`);
    process.exit(1);
  }

  // Server is now listening on ${address}
  console.log("RUNNING");
  server.log.info(`Server listening on ${address}`);
});

tsconfig

{
  "compilerOptions": {
    "target": "ES2017",
    "module": "commonjs",
    "strict": true,
    "declaration": true,
    "esModuleInterop": true,
    "noImplicitAny": false,
    "allowSyntheticDefaultImports": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true,
    "outDir": "./dist",
    "baseUrl": ".",
    "paths": {
      "~/*": ["./src/*"],
      "@apiv1/*": ["./src/api/*"]
    }
  },
  "include": ["src/**/*.ts"],
  "exclude": ["node_modules", "**/*.d.ts"]
}

package json

{
  "name": "backend-fastify",
  "version": "1.0.0",
  "main": "src/server.ts",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "prod": "ts-node src/server.ts",
    "dev": "nodemon -r ts-node/register -r tsconfig-paths/register src/server.ts",
    "build": "tsc -p tsconfig.json"
  },
  "author": "",
  "license": "ISC",
  "description": "",
  "types": "index.d.ts",
  "dependencies": {
    "@fastify/cors": "^9.0.1",
    "@fastify/jwt": "^8.0.1",
    "@fastify/mongodb": "^8.0.0",
    "@types/node": "^20.14.7",
    "@types/passport-jwt": "^4.0.1",
    "dotenv": "^16.4.5",
    "fastify": "^4.28.0",
    "fastify-plugin": "^4.5.1",
    "mongoose": "^8.4.3",
    "neo4j-driver": "^5.21.0",
    "passport": "^0.7.0",
    "passport-jwt": "^4.0.1"
  },
  "devDependencies": {
    "nodemon": "^3.1.4",
    "ts-node": "^10.9.2",
    "tsconfig-paths": "^4.2.0",
    "typescript": "^5.5.2"
  }
}

We have no function called __createBinding.

You are probably transpiled the files, and using the JavaScript version.

Can you place all the above in a repo we can clone? Your error does not match your files:

src/api/plugins/index.ts contains something that's not included here.

this turned out to be some misconfiguration with typescript, and nothing to do with fastify.
thanks for support regardless.