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

602
Vistas
Mocha / Webpack: Cannot use import statement outside a module

I'm new to Node and JS Testing. I have a web applications w/ Webpack as a bundler. I have some entry point JS's which are included into the page. The entry points are using module files like this:

export default function() {
    ...
}

Now I would like to Unit test this module. I have picked up Mocha but it is not critical to me. Could be Jest or anything else.

I wrote a very simple test.js like this. It it not doing anything but tests if the entire setup works:

import foo from '../js/someModuleOfMine'

const assert = require('assert')

describe('Test Suite', () => {
    it('should at least run', () => {
        assert.equal(true, true)
    })
})

executing mocha from CLI gives me this error:

import foo from '../js/someModuleOfMine'
^^^^^^

SyntaxError: Cannot use import statement outside a module

Adhering to some advises I have tired to add "type": "module" to my package.json but it only changed error to something even more obscure:

Error: Not supported

I am definitely missing something obvious but I cannot comprehend what.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Not sure if it will help the OP, but I had the same problem and it was due to typescript. I solved in this way:

install ts-node:

npm install ts-node --save-dev

add the require line in the mocha config (I have it in the package.json, but one can also have it in the .mocharc.json file):

  "mocha": {
    "spec": "./**/*test.ts",
    "ignore": "./node_modules/**",
    "require": "ts-node/register/files",
    "timeout": 20000
  }
about 4 years ago · Juan Pablo Isaza Denunciar
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