Vuejs code must run in a browser environment in which you don't have access to your hardware. So basically you have three options to achieve this:
1. Use Electron
Electron gives you a chromium instance in which your UI (Vuejs) code can run and a separate process in which you can have access to your hardware using nodejs.
2. Create a simple http server with node
In this case you need to create a server with nodejs. For example you can create a file named server.js
like this:
const http = require('http');
const fs = require('fs')
http.createServer((req, res) => {
// Do something with fs
const data = fs.readFileSync('path/to/some/file');
// return back the data with http
http.writeHead(200, {ContentType: 'text/plain'})
http.write(data);
}).listen(8000)
And fetch the data in your Vuejs app with http like this:
export default {
data () {
return {
myData: null
}
},
methods: {
onSomeAction () {
const {data} = await axios.get('localhost:8000')
this.myData = data;
}
}
}
Now in your package.json you can put this to run both when npm serve
is called:
...
"scripts": {
...
"serve": "node server.js & vue-cli-service serve"
}
3. Use Nuxt
In case you need to do something with your hardware ONCE, and BEFORE the render you can use Nuxt to access the hardware before rendering the Vuejs app and handing it over to your browser.