Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

476
Views
How to configure Relay.JS in Vite?

I'm trying to migrate my React project from CRA to Vite, this is my vite.config.js:

import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import envCompatible from 'vite-plugin-env-compatible'
import relay from "vite-plugin-relay"
import macrosPlugin from "vite-plugin-babel-macros"
import path from 'path';
import fs from 'fs/promises';

export default defineConfig({
  resolve: {
    alias: {
      '~': path.resolve(__dirname, 'src'),
      '@material-ui/core': path.resolve(__dirname, 'node_modules/@material-ui/core')
    }
  },
  esbuild: {
    loader: "tsx",
    include: /src\/.*\.[tj]sx?$/,
    exclude: [],
  },
  optimizeDeps: {
    esbuildOptions: {
      plugins: [
        {
          name: "load-js-files-as-jsx",
          setup(build) {
            build.onLoad({ filter: /src\/.*\.js$/ }, async (args) => ({
              loader: "tsx",
              contents: await fs.readFile(args.path, "utf8"),
            }));
          },
        },
      ],
    },
  },
  define: {
    global: {},
  },
  plugins: [
    envCompatible(),
    react(),
    relay,
    //macrosPlugin(),
  ],
})

My GraphQL query files are like this:

import graphql from 'babel-plugin-relay/macro'

const getQuery = () => graphql`
    query UserQuery($id: ID!) {
      user(id: $id) {
        id
        fullName
      }
    }
  `

export default getQuery

When I tried to run the project in dev mode (command $ vite), I got this error:

Preset /* your preset */ requires a filename to be set when babel is called directly error message

So I did some search and replaced vite-plugin-relay to vite-plugin-babel-macros like this:

// others import
import relay from "vite-plugin-relay"
import macrosPlugin from "vite-plugin-babel-macros"

export default defineConfig({
  // configs like bellow
  plugins: [
    envCompatible(),
    react(),
    //relay,
    macrosPlugin(),
  ],
})

So I started to get a new error:

ReferenceError: require is not defined error message

How can I configure Relay to work on Vite.JS?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Might be a bit late, but the issue has been fixed in Relay@13 and you can find some workarounds in this thread for older versions of Relay :

https://github.com/facebook/relay/issues/3354

You can also try adding the option eagerEsModules: true to your relay babel plugin configuration.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!