So I am trying to import a nodejs library net
into a vuejs project. But I cannot import the library. I have tried using const net = require('net')
, const net = require('net').default
, and import * as net from 'net'
however none of these work and the object is empty (if i console.log() it, or throws Uncaught TypeError: Cannot read property 'createServer' of undefined
. I have also tried using both yarn
and npm
with no luck.
The net
module is definitely in my node_modules/
folder.
Component file
<template>
<p>charts placeholder</p>
</template>
<script>
const net = require('net').default;
console.log(net);
const port = 8080;
const host = '127.0.0.1';
let server = net.createServer(function(socket) {
socket.on('data', function(data){
let str = data.toString();
console.log(str);
try {
let json = JSON.parse(str);
console.log(json);
} catch (e) {
console.log('error str: ' + str);
}
});
socket.on('error', function(err) {
console.log(err)
})
});
server.listen(port, host);
export default {
name: "Charts"
}
</script>
<style scoped>
</style>
Any advice would be great thanks.
This is not the point of the frontend framework. If you want to make data available to your Vue application, you should use a separate project and run them separately.
To exchange data between your frontend and backend you can use axios or others: https://v2.vuejs.org/v2/cookbook/using-axios-to-consume-apis.html#ad