Skip to content
Advertisement

How can I run a node script in a Vite React project

I am trying to build a little SPA where a user can run a Presto query, I am using a Node presto client. When running the script via node it works without any issues. I am trying to implement it now via Vite

JavaScript

This is how I currently have it set up. When running the script via a React FE like so..

JavaScript

I am getting an error in the browser like so index.js:4 Uncaught ReferenceError: __dirname is not defined

I have "type": "module", in my package.json but I have also tried the following var presto = require('presto-client'); but in the browser I get required is not defined.

Therefore is it possible to run a node script like this, and if so how. This is how my src folder is

JavaScript

Advertisement

Answer

Vite is a frontend toolkit, it does not execute backend code, for that you would have to send a request to your backend

JavaScript

then on your backend you’d have an API route configured that points to a class/function that will execute the query, using a short, (untested) expressJS example.

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement