reactjsnode.jsvitepackage.json

Where do I set NODE_OPTIONS when using Vite and ReactJS?


I need to increase the Maximum Http Header Size for some of the API calls on my website. When I run the function in question, I get this error:

Server responded with status code 431. See https://vitejs.dev/guide/troubleshooting.html#_431-request-header-fields-too-large.

However, if I simply reduce the length of the input, it works perfectly fine. It's not a cookies issue, I'm just sending back a very large string(think a long article), but when it reaches a certain size, it no longer allows the user to submit it. So I need to use the option listed in the link the terminal provided(https://vitejs.dev/guide/troubleshooting.html#_431-request-header-fields-too-large) to increase the max-http-header-size. However, I don't know where to do this.

In the package.json, there is a scripts section containing this:

  "scripts": {
    "dev": "vite",
    "build": "vite build",
    "lint": "eslint . --ext js,jsx --report-unused-disable-directives --max-warnings 0",
    "preview": "vite preview"
  },

The startup script is "npm run dev". So I tried to simply modify the "dev" startup script:

  "scripts": {
    "dev": "vite NODE_OPTIONS=--max_http_header_size=128000",
    "build": "vite build",
    "lint": "eslint . --ext js,jsx --report-unused-disable-directives --max-warnings 0",
    "preview": "vite preview"
  },

This doesn't work, and the website simply doesn't work at all. So I tried to modify the startup script from:

npm run dev

To:

npu NODE_OPTIONS=--max_http_header_size=128000 run dev

But that also didn't work. I just need to know how to set the NODE_OPTION for max_http_header_size when using vite, since "vite" replaces "node" as the binary to run. For regular React-JS websites, the startup script is "node", and you can simply put the NODE_OPTIONS after that. But since we're using Vite, the startup script isn't "node" and adding the NODE_OPTIONS after "vite" doesn't work.


Solution

  • NODE_OPTIONS is environment variable, it's intended to inject node options. For a cross-platform project, cross-env dependency can be used to set it:

    cross-env NODE_OPTIONS=--max_http_header_size=128000 vite
    

    Alternatively, the command that stands for vite can be used instead:

    node --max_http_header_size=128000 node_modules/vite/bin/vite.js