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

352
Views
How to Convert, in JS, an import statement to a require() statement

I have a Statement in JavaScript:

import app, { set } from '../app';

How can I write an equivalent statement in a form, such as I don't get an error like 'Cannot use import statement outside a module'.

I could convert similar less complex statement by using a format: var Foo = require(foo);

but don't understand how to convert this one.

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

0

Not every import can be converted into a require. In pure Node.js this is only possible if the module being required is a CommonJS module, but the situation is different with tools such as Webpack or Babel.

I image you are using one of those tools, otherwise you would not be able to import ../app without a file extension in the first place: you would need to import from ../app.js or ../app/index.js explicitly, or to use export mappings, but that's a different story.

Then if

import app, { set } from '../app';

is a valid import in a ES module, with whatever loader, the equivalent CommonJS syntax would be

const app = require('../app');
const { set } = app;
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!