Trouble Dynamically Loading JSON in Next.js via NPM Package

I have a TypeScript project that dynamically loads a json configuration file based on the value of an enum passed as a parameter to my getInstance function in my PlatformConfigurationFactory file.

  public static async getInstance(
    platform: Platform
  ): Promise<PlatformConfiguration> {
    const jsonData = await this.configurationRepository.getConfig(platform);
    if (!jsonData) {
      throw new Error("Configuration data not found");
    }

    this.jsonValidator.loadSchema();
    if (!this.jsonValidator.isValid(jsonData)) {
      throw new Error("Invalid JSON data according to schema");
    }

    return this.fromJson(jsonData);
  }

The JsonConfigurationRepository file is used to read the file and return its contents in Json.

import { IConfigurationRepository } from "../../application/interfaces/IJsonConfigurationRepository";
import { Platform } from "../../core/domain/Platform";
import fs from "fs/promises";
import path from "path";

export class JsonConfigurationRepository implements IConfigurationRepository {
  async getConfig(platform: Platform): Promise<any> {
    const fileName = `${platform}-config.json`;
    const filePath = path.resolve(__dirname, "../../data", fileName);

    try {
      const fileContent = await fs.readFile(filePath, "utf-8");
      return JSON.parse(fileContent);
    } catch (error) {
      console.error(`Error loading configuration for ${platform}:`, error);
      return null;
    }
  }
}

The files are stored in ./src/data directory and when TypeScript builds the files in the ./dist folder, the json files are present thanks to my configuration below.

{
  "compilerOptions": {
    "target": "ESNext",                          
    "experimentalDecorators": true,              
    "emitDecoratorMetadata": true,               
    "module": "ESNext",                          
    "rootDir": "./src",                          
    "moduleResolution": "Node",                  
    "baseUrl": "./",                             
    "paths": {                                   
      "@mylib/*": ["src/*"]
    },                                      
    "resolveJsonModule": true,                   
    "declaration": true,                         
    "declarationMap": true,                      
    "outDir": "./dist",                          
    "skipLibCheck": true                         
  },
  "include": [
    "src/**/*",
    "data/**/*.json",
  ],
  "exclude": [
    "node_modules",
    "**/*.spec.ts",
    "**/*.test.ts"
  ]
}

The tests are ok, when I call my factory it returns the configuration of a platform that I pass as a parameter :

    const config = await PlatformConfigurationFactory.getInstance(
      Platform.Facebook
    );

This project is then stored in Gitlab’s Package Registry as an NPM package, which I install in a Next.js 14 project via npm i @mylib/platform-configurations.

But when I have an error when I call the same code in Next.js :

    const config = await PlatformConfigurationFactory.getInstance(
      Platform.Facebook
    );

Error:

Error loading configuration for facebook: [Error: ENOENT: no such file or directory, open ‘/Users/me/workspace/nextjs/.next/server/json/facebook-config.json’] {
errno: -2,
code: ‘ENOENT’,
syscall: ‘open’,
path: ‘/Users/me/workspace/nextjs/.next/server/json/facebook-config.json’
}
⨯ Error: Configuration data not found
at Generator.next ()
digest: “2762426514”
⨯ Error: Configuration data not found
at Generator.next ()
digest: “2762426514”

The code tries to load the file in the wrong path instead of loading the file in dist/data in the installed package. How can I fix the problem please?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật