I try to use my Room.js in my index.html on the client side of my Node.js server(v10.15.0 + express, socket.io). I keep getting errors. When I load it via script tags in the index.html file, the console gives me a
Loading failed for "script" with .. "Room.js".
ReferenceError: Room is not defined
When I use require in the app.js like this:
const roomObj = require("./client/js/Room.js");
const Room = new roomObj();
I only get this Error
ReferenceError: Room is not defined
module.exports = Room;
function Room(name, o, obs, ep){
this.name = name;
this.o = o;
this.obs = obs;
this.getSth = function(ep){}
this.loadRoom = function(){}
}
<script src="./js/Room.js"></script>
<script>
function loadRoom(){
let room = new Room(name, o, obs, ep);
room.loadRoom();
}
</script>
Update
Getting 404 Error for Room.js
When accessing localhost:2000/js/ and following subdirectories I got the
Cannot GET /js/ and the same result for every other Path
Solution: (from Comments in answer)
replaced line 5-7 in app.js with
app.use(express.static(__dirname + "/client"));
Room.js
in the same directory asindex.html
on the web server? Do you use Webpack or other build tool?'module' is not defined
error.