How to use “require” in a modern Javascript? Standard approaches not working

I have a react, vite project that is in typescript and of “type: module”, however, I need to call a one-of-a-kind file that is built with “require” syntax rather than “import”. I can’t update it so I have to get the two working together and I’ve been unable to.

I’ve built a “wrapper.js” file that contains the following code to try and convert the older format to the import format for use:

const {getProduct, getProducts} = require("../../../server/productInfo");
export {getProduct, getProducts};

But with the above code, I get the error:

ReferenceError: require is not defined

So, following internet solutions I added this at the top of the file:

import { createRequire } from "module"; // "node:module"
const require = createRequire(import.meta.url);

But that give’s me this new error:

SyntaxError: The requested module '/node_modules/.vite/deps/module.js?v=21b36cad' does not provide an export named 'createRequire'

So, then I tried importing module as default, but that seemed to be null

import module from "module";
console.log(module) // null

I then tried to polyfill it like below.

// vite.config.ts
import { defineConfig } from 'vite'
import { nodePolyfills } from 'vite-plugin-node-polyfills'
import pkg from './package.json' assert { type: 'json' }
import react from '@vitejs/plugin-react-swc'

export default defineConfig({
  plugins: [
    nodePolyfills(),
    react({ plugins: [['@swc/plugin-styled-components', {}]] }),
  ],
  define: {
    APP_VERSION: `"${pkg.version}"`,
    APP_TITLE: `"${pkg.displayName}"`
  },
  build: {
    outDir: '../server/frontend/dist',
  },
})

But I’m unsure how to use it. Because if I do the below, I still get a “require is not defined” error.

import { createRequire } from "module";
const require = createRequire(import.meta.url);

Does anyone know what I’m missing or if I’m making a syntax mistake?

FYI, my tsconfig.json looks like this:

{
  "compilerOptions": {
    "target": "es5",
    "lib": ["DOM", "DOM.Iterable", "ESNext"],
    "module": "ESNext", //"CommonJS",
    "skipLibCheck": true,
    "outDir": "../server/frontend/dist",
    "moduleResolution": "node",
    "allowImportingTsExtensions": true,
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react-jsx",
    "allowJs": true,
    "checkJs": false,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noFallthroughCasesInSwitch": true,
    "typeRoots": ["./node_modules/@types", "./src/types"],
    "types": ["vite/client", "node"]
  },
  "exclude": [
    "node_modules",
    "**/node_modules/*",
    "**/dist/*",
  ],
  "ts-node": {
    "swc": true,
    "esm": true,
    "compilerOptions": {
      "module": "ESNext" //"CommonJS"
    }
  }
}

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