Meliodas278
Meliodas278

Reputation: 21

"fs is not defined" How can I use fs within a VueJS file

I have been researching online and many others have ran in to similar problems that were left unresolved.

Why is that I can't use fs in VueJS file, I have read that it cannot be called on the client side however it is used within the docs so what am I doing wrong?

https://socket.io/docs/client-api/#With-a-self-signed-certificate

socket.js file:


const fs = require('fs');

var https = require('https').createServer(app, {
  key: fs.readFileSync('server-key.pem'),
  cert: fs.readFileSync('server-cert.pem'),
  passphrase: ''
});

chat.js file:

import io from 'socket.io-client';

const socket = io('my_public_ip:port', {
  ca: fs.readFileSync('server-cert.pem'),  //unable to read fs here
});

Upvotes: 1

Views: 5856

Answers (2)

Leonardo Quevedo
Leonardo Quevedo

Reputation: 53

It seems to me that this is rather a misconception regarding the concept of "client":

  • "Client", in socket terms, can be any software able to connect to the socket server (eg.: servers, browsers, mobile apps, games...)

  • "Server", in socket terms, is the agent responsible for accepting connections, gathering the clients.

As per the module (fs) itself, it does not work on the browser environment because it depends on the Node.js core, which comes bundled on the Node.js installation packages and is only available on Node.js servers.

On the browser side, in order to connect to secure (https) socket servers, all you need to do is set the secure parameter while connecting, as in:

const socket = io.connect('my_public_ip:port', { secure: true });

Upvotes: 1

Anatoly
Anatoly

Reputation: 22803

fs is a module in the Node.js and it is not supported in a browser to forbid a direct file system operations due to security reasons.

Upvotes: 0

Related Questions