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

130
Views
Having trouble converting this require statement into an import statement

I have a require statement in my script.

const packageJsonVersion = require('../package.json').version;

If I try and run the script I get an error saying I need to convert it to an import statement.

I changed the code to import { version as packageJsonVersion } from '../package.json' but when I run the script I get the following error.

TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".json" for /home/alex/_code/connect/package.json

I'm not sure how to get around this.

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

0

It depends on what environment you're doing this in.

  • Right now, browsers don't natively support JSON modules (though it's coming).
  • If you're doing this with a bundler, you need to be sure to tell the bundler how to handle the .json extension.
  • If you're doing this in Node.js, you need the JSON module notation (assert {type: "json"}).

Separately, though, JSON modules won't support named exports (this is mentioned by both links above). You'll have to import the default and then use the version property on it:

import data from "../package.json" assert { type: "json" };
const packageJsonVersion = data.version;
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!