Home > database >  Problems running imported JS in EJS file
Problems running imported JS in EJS file

Time:09-10

I made an express Nodejs server and set the static folder. The CSS has access to the page, but the JS does not find any elements. Everything is NULL.

app.use(express.static(__dirname   '/static'))

app.get('/', (req, res) => {
    res.render('index')
})

//test from clientside index.js
document.body.append('test')
<script src="js/index.js"></script>
<link rel="stylesheet" href="style/main.css">

What am I doing wrong?

CodePudding user response:

Problem

DOM doesn't load yet when your JS runs. It's not related to EJS, that's just how every script works.

Solution

Add defer attribute

<script defer src="js/index.js"></script>
  • Related