59

The page is rendered but it gives an error when I press any link.

ReferenceError: path is not defined
at app.get (/var/www/example.com/example-domain/server.js:106:19)
at Layer.handle [as handle_request] (/var/www/example.com/example-domain/node_modules/express/lib/router/layer.js:95:5)
at next (/var/www/example.com/example-domain/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/var/www/example.com/example-domain/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/var/www/example.com/example-domain/node_modules/express/lib/router/layer.js:95:5)
at /var/www/example.com/example-domain/node_modules/express/lib/router/index.js:281:22
at param (/var/www/example.com/example-domain/node_modules/express/lib/router/index.js:354:14)
at param (/var/www/example.com/example-domain/node_modules/express/lib/router/index.js:365:14)
at Function.process_params (/var/www/example.com/example-domain/node_modules/express/lib/router/index.js:410:3)
at next (/var/www/example.com/example-domain/node_modules/express/lib/router/index.js:275:10)

Here is the code from server.js:

app.use(express.static('../build'));
app.get('*', (req, res)=> {
  const index = path.join(__dirname, '/', '../build', 'index.html' );
  res.sendFile(index);
});

Thanks in advance.

BENARD Patrick
  • 30,363
  • 16
  • 99
  • 105
EmanuelGF
  • 645
  • 1
  • 6
  • 15

4 Answers4

145

In the head of your file, just add

const path = require('path');
BENARD Patrick
  • 30,363
  • 16
  • 99
  • 105
  • With NodeJS 18 path seems to be define without requiring it. However ESLint still complains. – Marc Jan 30 '23 at 08:26
19

did you require path module

const path = require('path')
M.Amer
  • 658
  • 6
  • 13
0

You need to include Path module in your file, The syntax for including the Path module in your application:

const path = require('path');
0

Write this in the header of your code. This line of code imports the built-in Node.js path module, which provides utilities for working with file and directory paths.

By requiring the path module, you can use its methods to manipulate file paths in a way that's safe and cross-platform compatible.

const path = require('path');
LakshyaK
  • 1
  • 3