Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

268
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda