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

598
Views
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 answers
Answer question

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 Report
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!