Search code examples
javascriptwebdriverbundlebrowserifyrequire

fs.readFileSync is not a function?


I'm trying to make a bot to run client-side and control a third-party page, but this error appears I am using a browser.

Uncaught TypeError: fs.readFileSync is not a function, 

here is my code

 global.apostar = function() {
    var fs = require('fs')//require
    var src = fs.readFileSync(__dirname + '/file.txt')
    const nodeExternals = require('webpack-node-externals')
    module.exports = {    
        entry: './bundle.js',
        target: 'node',
        externals: [nodeExternals()],
        output: {
            path: path.join(__dirname, 'build'),
            filename: 'bundle.js'
      }
    }
    const auxiliar = require(electron)
    const{Builder, By, Key, util} = require('selenium-webdriver')
    async function example() {//construir o chrome webdriver
        let driver = await new Builder().forBrowser("firefox").build()
        await driver.get("http://google.com")
        await driver.findElement(By.name("q")).sendKeys("Selenium",Key.RETURN)
    }
    example()
        //call function
}

Solution

  • You can not use "fs" in browsers because it is a Node.js specific library that enables file system operations on OS level. Browser do not support these low-level APIs so you can unfortunately only use it in Node.js.

    Google currently develops a browser based file system api, maybe this helps.

    Btw Selenium will also not work because it spawns another browser again with OS level APIs.