index.js logging the view paths to ensure partials are visable
import express from 'express';
import bodyParser from 'body-parser';
import path from 'path';
import { fileURLToPath } from 'url';
import { dirname } from 'path';
import mustacheExpress from 'mustache-express';
import dotenv from 'dotenv';
import homeRouter from './home/router.js';
import jobsRouter from './jobs/router.js';
import errors from './errors/errors.js';
dotenv.config();
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const app = express();
// Middleware
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static(path.join(__dirname, '..', 'public')));
// Configure mustache
app.set('views', [
path.join(__dirname, 'home'), // Home templates
path.join(__dirname, 'jobs'), // Jobs templates
path.join(__dirname, 'errors'), // Error templates
path.join(__dirname, 'site/partial-views'),
]);
console.log('Views set to:', app.get('views'));
app.set('view engine', 'mustache');
app.engine('mustache', mustacheExpress());
// Routes
app.use('/', homeRouter);
app.use('/jobs', jobsRouter);
// Error handling middleware
app.use(errors.notFound);
app.use(errors.internalServerError);
app.use(errors.emailError);
export default app;
terminal with views log
[nodemon] starting `node ./app/server.js`
Views set to: [
'C:\Users\Keegan\source\repos\my-website\app\home',
'C:\Users\Keegan\source\repos\my-website\app\jobs',
'C:\Users\Keegan\source\repos\my-website\app\errors',
'C:\Users\Keegan\source\repos\my-website\app\site\partial-views'
]
Server running on port 3000
Connected to MySQL database
terminal with error
node:internal/errors:540
throw error;
^
TypeError [ERR_INVALID_ARG_TYPE]: The "paths[0]" argument must be of type string. Received an instance of Array
at Object.resolve (node:path:198:9)
at C:UsersKeegansourcereposmy-websitenode_modulesmustache-expressmustache-express.js:99:24
at C:UsersKeegansourcereposmy-websitenode_modulesasyncdistasync.js:247:13
at eachOfArrayLike (C:UsersKeegansourcereposmy-websitenode_modulesasyncdistasync.js:507:13)
at eachOf (C:UsersKeegansourcereposmy-websitenode_modulesasyncdistasync.js:627:16)
at awaitable (C:UsersKeegansourcereposmy-websitenode_modulesasyncdistasync.js:212:32)
at _asyncMap (C:UsersKeegansourcereposmy-websitenode_modulesasyncdistasync.js:245:16)
at Object.map (C:UsersKeegansourcereposmy-websitenode_modulesasyncdistasync.js:750:16)
at Object.awaitable [as map] (C:UsersKeegansourcereposmy-websitenode_modulesasyncdistasync.js:212:32)
at loadAllPartials (C:UsersKeegansourcereposmy-websitenode_modulesmustache-expressmustache-express.js:94:8) {
code: 'ERR_INVALID_ARG_TYPE'
}
Node.js v22.12.0
[nodemon] app crashed - waiting for file changes before starting...
directory
my-website/
----app/
--------index.js
--------server.js
--------db.js
--------config.js
--------jobs/router.js, jobs.mustache
--------home/router.js, home.mustache
--------errors/errors.js, erorrs.mustache
--------site/partial-views/head.mustache
----public/
home.mustache
<html lang="en" data-bs-theme="auto">
{{> head}}
</html>
home/router.js
import express from 'express';
import connection from '../db.js';
const router = express.Router();
router.get('/', (req, res, next) => {
const query = 'SELECT id, title, location, salary, posted FROM jobs';
connection.query(query, (err, results) => {
if (err) {
return next(err);
}
res.render('home', { jobs: results });
});
});
export default router;
home.mustache is currently the only template attempting to use the partial. Removing the 1 line {{> head}} in home.mustache and manually copying the contents of head.mustache in its place removes the error, and thewebsite renders the ‘/’ root endpoint correctly.
Thank you for your time and feedback !