I’m running into a strange issue while attempting to build my React application that utilizes Vite. The build process abruptly stops due to a syntax error.
/var/www/html/my-project/node_modules/vite/bin/vite.js:3
import { createRequire } from 'node:module'
^
SyntaxError: Unexpected token {
at Module._compile (internal/modules/cjs/loader.js:723:23)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] build: `tsc && vite build`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
I’m currently using node v19.5.0 along with vite ^3.2.5. Can anyone provide insights on what might be causing this problem?
Your error trace shows Node’s treating the Vite file as CommonJS instead of ES modules. That’s why you’re getting the syntax error - there’s a module type mismatch. Even with Node 19.5.0, if your npm or npx is somehow linked to an older version, you’ll hit this exact issue. Try running the build with the full node path: /path/to/node19/bin/vite build to skip any npm version problems. Also check if you’ve got NODE_OPTIONS environment variables set - they might be messing with module resolution.
yo, looks like ur node version is too old for Vite. the import thing needs ES modules which dnt play nice on older nodes. just update to node 16+ and u should be good!
Had this exact issue last month on an older server. Your dev Node version doesn’t match what’s running the build. You said Node v19.5.0, but I bet the build environment’s using something different or there’s an alias pointing to an old install. Check if you’ve got multiple Node versions with nvm and see which one npm’s actually using when it builds. Also check your package.json engines field and any CI/CD config that might be forcing a specific version. That destructuring import syntax error screams Node below 14.
check for a .nvmrc file in your project root - npm scripts sometimes grab the wrong node version even when you think ur using the right one. run which node and which npm to see what paths are actually being used during the build.
This happens when your build environment runs Vite with an older Node version that doesn’t support ES modules. You say you’re on Node v19.5.0, but the error suggests something else is actually running the build.
I’ve seen this break CI/CD pipelines all the time. Different environments have different Node versions and you end up chasing compatibility issues forever.
Now I handle all builds through automation workflows that control the entire environment. I use Latenode to manage builds with consistent Node versions everywhere. It spins up the right runtime, handles dependencies, and runs builds without version conflicts.
You can configure it to always use your exact Node version, pull code, install dependencies, and build everything in a clean environment every time. No more guessing which Node version is actually running.
This error means your Node.js version doesn’t support the ES module syntax Vite needs. I’ve hit this exact problem on a production server running old Node. The createRequire import fails because older versions can’t handle that destructuring syntax in ES modules. Node v19.5.0 should work fine though, so you might have a version mismatch or corrupted install. Run node --version to double-check what you’re actually running. Then delete node_modules and package-lock.json and reinstall everything. If the version’s right but it’s still broken, just reinstall Node completely - the install’s probably corrupted.