fs

How to read a text file and return it as a JSON object in Node JS?

妖精的绣舞 提交于 2021-02-19 05:22:12
问题 I have a text file. I need to read the file inside a function and return it as a JSON object. The following is throwing an error "Unexpected token V in JSON at position 0" . Server.js fs.readfile('result.txt', 'utf8', function(err,data) { if(err) throw err; obj = JSON.parse(data); console.log(obj); }); result.txt looks like the following VO1: 10 5 2 VO2: 5 3 2 I think I cannot use JSON.parse directly. How do I proceed? 回答1: Assuming the following: Every line is separated by a newline

Difference between readFileSync and using promisify on top of readFile with async/await

天大地大妈咪最大 提交于 2021-02-16 16:19:12
问题 Out of curiosity, i want to know if there is any difference between the two. readFileSync: function parseFile(filePath) { let data = fs.readFileSync(filePath); } readFile with promisify: const readFilePromise = promisify(fs.readFile); async function parseFile(filePath) { let data = await readFilePromise(filePath); } If you need some context, im trying to read a bunch of files in a folder, replace a lot of values in each one, and write it again. I don`t know if there is any difference in using

Fetch blob from URL and write to file

纵饮孤独 提交于 2021-02-08 18:31:18
问题 I'm trying to fetch some binary data (MP3) from a server and then store it in a file: var fs = require ('fs'); var fetch = require ('node-fetch'); fetch ( audioUrl, { method: 'GET', headers: { 'Accept': '*/*' } } ) .then ((res) => res.blob()) .then ((blob) => { console.log ('Blob size: ', blob.size); fs.writeFile ( `filename.mp3`, blob, // BUG HERE (err) => { if (err) { console.log (`Error writing audio ${audioIdx}`, err); } } ); }) The problem is marked at BUG HERE . I'm passing a blob to fs

Fetch blob from URL and write to file

心已入冬 提交于 2021-02-08 18:29:57
问题 I'm trying to fetch some binary data (MP3) from a server and then store it in a file: var fs = require ('fs'); var fetch = require ('node-fetch'); fetch ( audioUrl, { method: 'GET', headers: { 'Accept': '*/*' } } ) .then ((res) => res.blob()) .then ((blob) => { console.log ('Blob size: ', blob.size); fs.writeFile ( `filename.mp3`, blob, // BUG HERE (err) => { if (err) { console.log (`Error writing audio ${audioIdx}`, err); } } ); }) The problem is marked at BUG HERE . I'm passing a blob to fs

fs.existsSync is not a function when used in electron

烈酒焚心 提交于 2021-02-08 15:12:46
问题 I am using Angular 10 , Electron 10.0 and electron-builder v22.8.0 . When starting my Electron app, I receive the following error in the console: fs.existsSync is not a function when used in electron getElectronPath @ ./node_modules/events/events.js:6 <anonymous> @ ./node_modules/events/events.js:17 ./node_modules/electron/index.js @ ./node_modules/events/events.js:19 __webpack_require__ @ ./webpack/bootstrap:79 ./src/app/projectview/new/new.component.ts @ ./src/app/projectview/new/new

fs.existsSync is not a function when used in electron

瘦欲@ 提交于 2021-02-08 15:11:18
问题 I am using Angular 10 , Electron 10.0 and electron-builder v22.8.0 . When starting my Electron app, I receive the following error in the console: fs.existsSync is not a function when used in electron getElectronPath @ ./node_modules/events/events.js:6 <anonymous> @ ./node_modules/events/events.js:17 ./node_modules/electron/index.js @ ./node_modules/events/events.js:19 __webpack_require__ @ ./webpack/bootstrap:79 ./src/app/projectview/new/new.component.ts @ ./src/app/projectview/new/new

`fs.mkdir` is creating the directory with different permissions than those specified

怎甘沉沦 提交于 2021-02-07 08:01:53
问题 Due to the modules my node program uses, it requires root to run. This is because it has to run a HTTP server on port 80 (and as you know, lower ports like that require root in order to be used). But, this program also uses a fs method to create a file. I don't mind if root is the owner as long as I can change the permissions... But that's exactly where I'm hitting the bump. I'm using the fs.mkdir function to create a directory like so: (Lets say this file is named create.js ) fs.mkdir(_

`fs.mkdir` is creating the directory with different permissions than those specified

早过忘川 提交于 2021-02-07 08:01:37
问题 Due to the modules my node program uses, it requires root to run. This is because it has to run a HTTP server on port 80 (and as you know, lower ports like that require root in order to be used). But, this program also uses a fs method to create a file. I don't mind if root is the owner as long as I can change the permissions... But that's exactly where I'm hitting the bump. I'm using the fs.mkdir function to create a directory like so: (Lets say this file is named create.js ) fs.mkdir(_

`fs.mkdir` is creating the directory with different permissions than those specified

谁说胖子不能爱 提交于 2021-02-07 08:00:34
问题 Due to the modules my node program uses, it requires root to run. This is because it has to run a HTTP server on port 80 (and as you know, lower ports like that require root in order to be used). But, this program also uses a fs method to create a file. I don't mind if root is the owner as long as I can change the permissions... But that's exactly where I'm hitting the bump. I'm using the fs.mkdir function to create a directory like so: (Lets say this file is named create.js ) fs.mkdir(_

Download image and resize in nodejs

随声附和 提交于 2021-01-29 17:50:33
问题 What I am trying to do is download an image from google into my system repository in the project folder /download . Next, I am trying to get the image from the download repository and resize and again save the resized image in /thumbnail repository. Below is the code which I have written //Google URL var mainuri = 'http://images.sadhguru.org/sites/default/files/media_files/iso/en/64083-natures-temples.jpg'; var dir = './download'; if (!fs.existsSync(dir)){ fs.mkdirSync(dir); } // CODE TO