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

872
Views
Mocha and Import Syntax: Cannot find module

Maintainer of multiple npm packages here. Been using mocha with the require syntax and wanting to migrate to the import syntax.

The error I am getting is

Cannot find module '<project>/src/index' imported from <project>/test/index.spec.js

Steps to Reproduce

With the following three files

  • src/index.js
export const sum = (a, b) => a + b;
  • test/index.spec.js
import { sum } from '../src/index';

const expect = require('chai').expect;

describe('Testing Index', () => {
    it('Testing sum', () => {
        expect(sum(7, 13)).to.equal(20);
    });
});
  • package.json
{
  "name": "mocha-debug",
  "type": "module",
  "main": "index.js",
  "scripts": {
    "test": "mocha \"./test/**/*.spec.js\""
  },
  "license": "ISC",
  "devDependencies": {
    "chai": "4.3.4",
    "mocha": "9.1.4"
  }
}

and using node v14.18.2, run yarn install and

yarn test

> `Cannot find module '<project>/src/index' imported from <project>/test/index.spec.js`

Notes

I've found a related issue that recommends using babel with --require @babel/register, but wasn't able to get over the error.

I've set up a test repo to make it easy to reproduce the issue

https://github.com/simlu/mocha-debug

Question

What am I doing wrong here? How do I get the tests to run successfully?

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!