title: SyntaxError: Unexpected token / in .... while compiling ejs | Node.js with MongoDB
body: <p>When I try and use <code>&lt;% include ./partials/messages %&gt;</code>, <code>&lt;%= include ./partials/messages %&gt;</code>, or even <code>&lt;%= include ./partials/messages { %&gt;</code>, among my file that contains </p>
  
  <pre><code>&lt;!-- REGISTER.EJS --&gt;
  
  &lt;div class="row mt-5"&gt;
      &lt;div class="col-md-6 m-auto"&gt;
          &lt;div class="card card-body"&gt;
              &lt;h1 class="text-center mb-3"&gt;
                  &lt;i class="fas fa-user-plus"&gt;&lt;/i&gt; Register
              &lt;/h1&gt;
              &lt;% include ./partials/messages %&gt;
              &lt;form action="/users/register" method="POST"&gt;
                  &lt;div class="form-group"&gt;
                      &lt;label for="name"&gt;Name&lt;/label&gt;
                      &lt;input
                          type="name"
                          id="name"
                          name="name"
                          class="form-control"
                          placeholder="Enter Name"
                          value="&lt;%= typeof name != 'undefined' ? name : '' %&gt;"
                      /&gt;
                  &lt;/div&gt;
                  &lt;div class="form-group"&gt;
                      &lt;label for="email"&gt;Email&lt;/label&gt;
                      &lt;input
                          type="email"
                          id="email"
                          name="email"
                          class="form-control"
                          placeholder="Enter Email"
                          value="&lt;%= typeof email != 'undefined' ? email : '' %&gt;"
                      /&gt;
                  &lt;/div&gt;
                  &lt;div class="form-group"&gt;
                      &lt;label for="password"&gt;Password&lt;/label&gt;
                      &lt;input
                          type="password"
                          id="password"
                          name="password"
                          class="form-control"
                          placeholder="Create Password"
                          value="&lt;%= typeof password != 'undefined' ? password : '' %&gt;"
                      /&gt;
                  &lt;/div&gt;
                  &lt;div class="form-group"&gt;
                      &lt;label for="password2"&gt;Confirm Password&lt;/label&gt;
                      &lt;input
                          type="password"
                          id="password2"
                          name="password2"
                          class="form-control"
                          placeholder="Confirm Password"
                          value="&lt;%= typeof password2 != 'undefined' ? password2 : '' %&gt;"
                      /&gt;
                  &lt;/div&gt;
                  &lt;button type="submit" class="btn btn-primary btn-block"&gt;
                      Register
                  &lt;/button&gt;
              &lt;/form&gt;
              &lt;p class="lead mt-4"&gt;Have An Account? &lt;a href="/users/login"&gt;Login&lt;/a&gt;&lt;/p&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  
  </code></pre>
  
  <p>I receive the follow error "SyntaxError: Unexpected token / in .... while compiling ejs". I am using this to add immediate feedback to users who try and register such as "password invalid" or "email already exist." I believe I have my messages.ejs file set up correctly as such:</p>
  
  <pre><code>&lt;% if(typeof errors != 'undefined') { %&gt;
  &lt;% errors.forEach(function(error) { %&gt;
    &lt;div class="alert alert-warning alert-dismissible fade show" role="alert"&gt;
      &lt;%= error.msg %&gt;
      &lt;button type="button" class="close" data-dismiss="alert" aria-label="Close"&gt;
        &lt;span aria-hidden="true"&gt;&amp;times;&lt;/span&gt;
      &lt;/button&gt;
    &lt;/div&gt;
  &lt;%}); %&gt;
  &lt;% } %&gt;
  
  &lt;% if(success_msg != '') { %&gt;
    &lt;div class="alert alert-success alert-dismissible fade show" role="alert"&gt;
      &lt;%= success_msg %&gt;
      &lt;button type="button" class="close" data-dismiss="alert" aria-label="Close"&gt;
        &lt;span aria-hidden="true"&gt;&amp;times;&lt;/span&gt;
      &lt;/button&gt;
    &lt;/div&gt;
  &lt;% } %&gt;
  
  &lt;% if(error_msg != '') { %&gt;
    &lt;div class="alert alert-warning alert-dismissible fade show" role="alert"&gt;
      &lt;%= error_msg %&gt;
      &lt;button type="button" class="close" data-dismiss="alert" aria-label="Close"&gt;
        &lt;span aria-hidden="true"&gt;&amp;times;&lt;/span&gt;
      &lt;/button&gt;
    &lt;/div&gt;
  &lt;% } %&gt;
  
  &lt;% if(error != '') { %&gt;
    &lt;div class="alert alert-warning alert-dismissible fade show" role="alert"&gt;
      &lt;%= error %&gt;
      &lt;button type="button" class="close" data-dismiss="alert" aria-label="Close"&gt;
        &lt;span aria-hidden="true"&gt;&amp;times;&lt;/span&gt;
      &lt;/button&gt;
    &lt;/div&gt;
  &lt;% } %&gt;
  </code></pre>
  
  <p>I am pretty lost. I believe I have added the <code>const example = require('example');</code>  files correctly. My app.js file looks like </p>
  
  <pre><code>const express = require('express');
  const expressLayouts = require('express-ejs-layouts');
  const mongoose = require('mongoose');
  const flash = require('connect-flash');
  const session = require('express-session');
  const passport = require('passport');
  
  const app = express();
  
  // Passport Config
  require('./config/passport')(passport);
  
  // DB Config
  const db = require('./config/keys').mongoURI;
  
  // Connect to MongoDB
  mongoose
    .connect(
      db,
      { useNewUrlParser: true }
    )
    .then(() =&gt; console.log('MongoDB Connected'))
    .catch(err =&gt; console.log(err));
  
  app.use(express.static( 'public' ));
  
  // EJS
  app.use(expressLayouts);
  app.set('view engine', 'ejs');
  
  // Bodyparser 
  app.use(express.urlencoded({ extended: false }));
  
  // Express Session
  app.use(session({
    secret: 'secret',
    resave: true,
    saveUninitialized: true
  }))
  
  // Passport Middleware
  app.use(passport.initialize());
  app.use(passport.session());
  
  // Connect Flash
  app.use(flash());
  
  // Global Variables
  app.use((req, res, next) =&gt; {
    res.locals.success_msg = req.flash('success_msg');
    res.locals.error_msg = req.flash('error_msg');
    res.locals.error = req.flash('error');
    next();
  });
  
  // Routes
  app.use('/', require('./routes/index'));
  app.use('/users', require('./routes/users'));
  
  const PORT = process.env.PORT || 5000;
  
  app.listen(PORT, console.log(`Server started on port ${PORT}`));
  </code></pre>
  
  <p>My file structure looks like: </p>
  
  <pre><code>&gt;config
       auth.js
       keys.js
       passport.js
  &gt;models
       User.js
  &gt;node_modules
  &gt;public
  &gt;routes
       index.js
       users.js
  &gt;views
       &gt;partials
          messages.ejs
       dashboard.ejs
       layout.ejs
       login.ejs
       register.ejs
       welcome.ejs
  .gitignore
  app.js
  package-lock.json
  package.json
  README.md
  </code></pre>
  
  <p>Does anyone have any ideas?</p>
  
  <p>Error message looks like this: </p>
  
  <pre><code>SyntaxError: Unexpected token / in C:\Users\jreed\Desktop\appproject\views\register.ejs while compiling ejs
  
  If the above error is not helpful, you may want to try EJS-Lint:
  https://github.com/RyanZim/EJS-Lint
  Or, if you meant to create an async function, pass `async: true` as an option.
      at new Function (&lt;anonymous&gt;)
      at Template.compile (C:\Users\jreed\Desktop\appproject\node_modules\ejs\lib\ejs.js:626:12)
      at Object.compile (C:\Users\jreed\Desktop\appproject\node_modules\ejs\lib\ejs.js:366:16)
      at handleCache (C:\Users\jreed\Desktop\appproject\node_modules\ejs\lib\ejs.js:215:18)
      at tryHandleCache (C:\Users\jreed\Desktop\appproject\node_modules\ejs\lib\ejs.js:254:16)
      at View.exports.renderFile [as engine] (C:\Users\jreed\Desktop\appproject\node_modules\ejs\lib\ejs.js:459:10)
      at View.render (C:\Users\jreed\Desktop\appproject\node_modules\express\lib\view.js:135:8)
      at tryRender (C:\Users\jreed\Desktop\appproject\node_modules\express\lib\application.js:640:10)
      at Function.render (C:\Users\jreed\Desktop\appproject\node_modules\express\lib\application.js:592:3)
      at ServerResponse.render (C:\Users\jreed\Desktop\appproject\node_modules\express\lib\response.js:1012:7)
      at ServerResponse.res.render (C:\Users\jreed\Desktop\appproject\node_modules\express-ejs-layouts\lib\express-layouts.js:77:18)
      at router.get (C:\Users\jreed\Desktop\appproject\routes\users.js:13:43)
      at Layer.handle [as handle_request] (C:\Users\jreed\Desktop\appproject\node_modules\express\lib\router\layer.js:95:5)
      at next (C:\Users\jreed\Desktop\appproject\node_modules\express\lib\router\route.js:137:13)
      at Route.dispatch (C:\Users\jreed\Desktop\appproject\node_modules\express\lib\router\route.js:112:3)
      at Layer.handle [as handle_request] (C:\Users\jreed\Desktop\appproject\node_modules\express\lib\router\layer.js:95:5)
  </code></pre>
  
  <p>Here is my users.js file: </p>
  
  <pre><code>&lt;!-- USERS.JS --&gt;
  
  const express = require('express');
  const router = express.Router();
  const bcrypt = require('bcryptjs');
  const passport = require('passport');
  
  // User Model
  const User = require('../models/User');
  
  // Login Page
  router.get('/login', (req, res) =&gt; res.render('login'));
  
  // Register Page
  router.get('/register', (req, res) =&gt; res.render('register'));
  
  // Register Handle
  router.post('/register', (req, res) =&gt; {
      const {name, email, password, password2} = req.body;
      let errors = [];
  
      // Check required fields
      if (!name || !email || !password || !password2) {
          errors.push({msg: 'Please enter all fields'});
      }
  
      // Check passwords match
      if (password != password2) {
          errors.push({msg: 'Passwords do not match'});
      }
  
      // Check password length
      if (password.length &lt; 6) {
          errors.push({msg: 'Password must be at least 6 characters'});
      }
  
      if (errors.length &gt; 0) {
          res.render('register', {errors, name, email, password, password2});
      } else { // Validation passed
          User.findOne({email: email}).then(user =&gt; {
              if (user) { // User Exists
                  errors.push({msg: 'Email is already registered'})
                  res.render('register', {errors, name, email, password, password2});
              } else {
                  const newUser = new User({
                      name,
                      email,
                      password
                  });
  
                  // Hash Password
                  bcrypt.genSalt(10, (err, salt) =&gt; bcrypt.hash(newUser.password, salt, (err, hash) =&gt; {
                      if(err) throw err;
                      // Set password to hashed
                      newUser.password = hash;
                      // Save user
                      newUser.save()
                          .then(user =&gt; {
                              req.flash('success_msg', 'Congratulations! You are now registered and can log in.');
                              res.redirect('/users/login');
                          })
                          .catch(err =&gt; console.log(err));
                  }))
              }
          });
      }
  
  });
  
  // Login Handle
  router.post('/login', (req, res, next) =&gt; {
      passport.authenticate('local',{
          successRedirect: '/dashboard',
          failureRedirect: '/users/login',
          failureFlash: true
      })(req, res, next);
  });
  
  // Logout Handle
  router.get('/logout', (req, res) =&gt; {
      req.logout();
      req.flash('success_msg', 'You are logged out.');
      res.redirect('/users/login');
  });
  
  module.exports = router;
  </code></pre>