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

105
Views
Cannot get basic index.js to work in simple mocha test project

I have a simple project laid out like:

project
│   package.json    
│
└───productA
│   │
│   └───test
│       │   spec1.js
│   
└───common
    index.js
    │
    └───sharedFunction1
        │   auth.js
        │   index.js
      

In spec1.js, I can successfully import a function from auth.js like this:

import { some_function } from "../../common/sharedFunction1/auth.js"

However, I thought that I should be able to use index.js files so that I can import like this:

import { some_function } from "../../common"

The function I am trying to import is something like (just a simple example):

export function get_file_listing (folder) {
    var files = fs.readdirSync(folder);
    return files
}

and my index.js under sharedFunction1 looks like:

export * from './auth';

and my index.js under common looks like:

export * from "./sharedFunction1";

VSCode auto import in the spec file seems to think the import should be:

import { some_function } from "../../common";

But when I run my test with that I get:

Error [ERR_UNSUPPORTED_DIR_IMPORT]: Directory import ... is not supported resolving ES modules imported from ...

My package.json has:

  "type": "module",

I have read a hundred different pages trying to understand what I am doing wrong, but am stuck. Can anyone tell me what I am doing wrong or if this should even work?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Found that I apparently need this file:

.mocharc.json

{
    "node-option": [
        "experimental-specifier-resolution=node"
    ]
}
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!