Problems running imported JS in EJS file

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?

Upvotes: 0

Views: 26

Answers (1)

Konrad
Konrad

Reputation: 24691

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>

Upvotes: 2

Related Questions