code_relic
code_relic

Reputation: 37

How to read contents of an SQL file in NodeJS?

I want to read the contents of an SQL file in NodeJS. Is there a simple way to do this? Even just to import the whole file as a string somehow would be good. It seems like a basic thing but I can't find any information on how to do it.

Any help would be greatly appreciated.

Upvotes: 0

Views: 2795

Answers (2)

code_relic
code_relic

Reputation: 37

It turns out you can simply use

const fs = require('fs');

...

fs.readFile('ny_name.sql', (err, data) => {
    if(err){
        console.log('Error: ', err)
    } else {
        console.log('Sql data: ', data)
    }
  });

in the same way you may read any readable file.

Upvotes: 2

Carsper Wu
Carsper Wu

Reputation: 1

you can try use XMLHttpRequest to import the file as a string.

https://developer.mozilla.org/zh-CN/docs/Web/API/XMLHttpRequest

Upvotes: 0

Related Questions