Chris Michael
Chris Michael

Reputation: 1627

fs.readFileSync is not a function when importing an object

I am developing in vuejs, focused on IPTV service. The problem I am having is that when I want to do the following

const {channels} = require('../lib/utils/index.js');

in the file actions.js it shows me the following error

index.js?c45a:26 Uncaught TypeError: fs.readFileSync is not a function
    at Module.eval (index.js?c45a:26)
    at eval (index.js:64)
    at Module../src/lib/utils/index.js (app.js:958)
    at __webpack_require__ (app.js:786)
    at fn (app.js:151)
    at eval (actions.js?63e0:2)
    at Module../src/store/actions.js (app.js:1005)
    at __webpack_require__ (app.js:786)
    at fn (app.js:151)
    at eval (index.js?4360:1)

Is there any way to solve this problem?

../lib/utils/index.js

const fs = require('fs');
const fetch = require('node-fetch');
const parser = require('iptv-playlist-parser');
const USM3U = require('./urls/us.m3u');


/*** 
 * @async
 * @method GM3U
 * @param {empty}
 * @Summary Generate US m3u file
 * @Description THIS FUNCTION SHOULD NOT BE USED.
 ***/
const GM3U = async() =>{
  const res = await fetch(`${USM3U}`);
  const data = await res.text();
  fs.writeFile('us.m3u' , data , (err) =>{
    if(err){
      console.log(err);
    }else{
      console.log('The file us.m3u was saved!');
    }
  })
};

const usPlaylist = fs.readFileSync('us.m3u' , {encoding: 'utf8'});
const channels = parser.parse(usPlaylist);


module.exports = {
  channels
};

actions.js

When I try to make the import I get the error Uncaught TypeError: fs.readFileSync is not a function

const {channels} = require('../lib/utils/index.js');

Upvotes: 2

Views: 5190

Answers (1)

L3Von
L3Von

Reputation: 21

You can't use fs package with Vue.js framework .Because fs is not frontend technology . Instead of that try to use AJAX call to solve your issue .

Upvotes: 2

Related Questions