Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

600
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda