Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

265
Views
Can't Import javascript class

I wan't to import a javascript class in my server.js file so I tried:
import Player from './classes/model/Player.js';

Problem then I get the error:
SyntaxError: Cannot use import statement outside a module

So I wrote this in my package.json:
"type": "module",

Problem then I get the error:
ReferenceError: require is not defined in ES module scope, you can use import instead

I think this is because I'm using require in my server.js file:

const express = require('express');
const http = require('http').Server(express);
const io = require('socket.io')(http, {
    cors: {
        origin: '*'
    },
    pingTimeout: 60000
});

Problem here is that I don't know how to use socket.io without this require syntax.
So I tried using require instead of import and without the "type":"module" like so:
const Player = require('./classes/model/Player.js')

Then I get the error:
SyntaxError: Unexpected token 'export'

So I removed the export default from my player class.
I don't get an error now, but my Player constant is just an empty json like so {}

I don't know what to do anymore...

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!