Not able to do import export functions in node.js project

I'm making a memory game, I want to export a function. logic.js
function incrementAttempts(){
attempts++;
attemptsHolder.textContent = attempts;
}
//Other functions-----

module.exports = {incrementAttempts}
function incrementAttempts(){
attempts++;
attemptsHolder.textContent = attempts;
}
//Other functions-----

module.exports = {incrementAttempts}
app.js
const incrementAttempts = require('./public/logic');
const incrementAttempts = require('./public/logic');
the app is keep crashing, i tried changing type to module in package.json, there was still no change
5 Replies
Joao
Joao9mo ago
What does the error say?
JOY
JOY9mo ago
const grid = document.querySelector('.gameGrid');
^
ReferenceError: document is not defined
[nodemon] app crashed - waiting for file changes before starting...
const grid = document.querySelector('.gameGrid');
^
ReferenceError: document is not defined
[nodemon] app crashed - waiting for file changes before starting...
Joao
Joao9mo ago
You're running on Node.js, which does not have access to the DOM. I would recommend looking into Vite to create something for the browser, instead.
JOY
JOY9mo ago
thanks is there any other way where i dont have to use vite ?
Joao
Joao9mo ago
You don't have to use Vite if you don't want to, it's just a convenience. You can use bare html, css and js files if you want to. There's also Webpack but I think that's much more complicated. To get started, it's as simple as running this on the terminal npm init vite@latest. This will give you a dev server and a few other niceties.