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

135
Views
How to import a file using ES6 modules, and read its content as a string?

In my standard React app, I need to achieve the following: import a file that sits within my src folder, in order to simply read its content as a string. For example, let's say I have the following code in a file:

alert('hey') 

then in some other file, I would like to do something like this, in pseudo code:

import * as string from './someFile.js'

console.log(string)

The output of the console.log should be the JS code, as a string:

alert('hey')

If I could place the file within my public folder, I'd be able to perform an http request and read it as I wish. But the problem is of course, that the file is part of the build process(inside the src folder)

Can this be done?

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

0

i can think about:

  1. define constants.js file with following code:

    export default "alert('vasia')";
    
  2. import this file from some react file:

    import vasia from "./constants";
    
    const App = () => {
      console.log(eval(vasia));
    }
    

is that what you r searching for?

But, must warn you: "eval" is evil!

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!