Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mstdmstd's avatar

How to use functions readFile and writeFile of "fs/promises" ?

In laravel 8 app I need to use functions readFile and writeFile of "fs/promises" I suppose this library is already installed with node js, so in resources/js/bootstrap.js I added line :

window.fs_promises = require('fs/promises');   

but I got error in npm line :

Module not found: Error: Can't resolve 'fs/promises' in '/home/vagrant/code/app/resources/js'

in package.json :

{
    "private": true,
    "scripts": {
        "dev": "npm run development",
        "development": "mix",
        "watch": "mix watch",
        "watch-poll": "mix watch -- --watch-options-poll=1000",
        "hot": "mix watch --hot",
        "prod": "npm run production",
        "production": "mix --production"
    },
    "devDependencies": {
        "@popperjs/core": "^2.10.2",
        "axios": "^0.21",
        "bootstrap": "^5.1.3",
        "laravel-mix": "^6.0.6",
        "lodash": "^4.17.19",
        "postcss": "^8.1.14",
        "resolve-url-loader": "^5.0.0",
        "sass": "^1.32.11",
        "sass-loader": "^11.0.1"
    },
    "dependencies": {
        "uuid": "^8.3.2"
    }
}

Do I lack some libraries ?

Thanks!

0 likes
5 replies
martinbean's avatar

@mstdmstd That‘s a Node module for reading and writing files on the server. Why are you trying to bundle this in client-side code?

martinbean's avatar

@mstdmstd That is not the approach. At all.

The fs module is for reading and writing files to a filesystem. On a server. You can’t do that in a browser.

I think you need to study whatever library/package it is you’re working with on how to extend it properly rather than trying to read and write actual source JavaScript files.

If you could edit a file from a browser, so could any one. Which is why it’s not possible; it’s a huge security risk.

1 like
mstdmstd's avatar

Anyway I think that way with genearating modified version of js file is valid. Otherwize uuid will geneaate unique custom_id for any session.

My app has :

vagrant@homestead:~/code/app$ node -v
v14.19.0
vagrant@homestead:~/code/app$ npm -v
8.5.2
vagrant@homestead:~/code/app$ php -v
PHP 8.1.3 (cli) (built: Feb 21 2022 14:48:42) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.3, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.3, Copyright (c), by Zend Technologies
vagrant@homestead:~/code/app$ php artisan --version
Laravel Framework 8.83.18

I tried different way of importing fs I found (like on https://stackoverflow.com/questions/64725249/fs-promises-api-in-typescript-not-compiling-in-javascript-correctly branch) but in all cases I got error :

ERROR in ./resources/js/bootstrap.js 20:0-36
Module not found: Error: Can't resolve 'fs' in '/home/vagrant/code/landigator/resources/js'
martinbean's avatar

@mstdmstd How many times do want telling? fs is a Node module that’s supposed to be used only on the server-side.

Please or to participate in this conversation.