Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to require 'fs' with Vue CLI 3

Tags:

node.js

vue.js

I am developing a scheduling software for Windows 10, using Vue CLI 3.

The app needs to use "fs" module, but .. I could not find the way. There is no webpack config file anywhere. How can I solve the problem?

Vue CLI 3 is so different that I cannot use all the ways introduced at stackoverflow.

Please, help me.

like image 656
Hwaryong Avatar asked Sep 20 '18 08:09

Hwaryong


People also ask

How to configure the Vue CLI server?

To configure the Vue CLI server, we need to create a vue.config.js file in the root of the frontend project right beside package.json, containing: // options... Inside the module.export you can add configurations for your development server using devServer object.

Does hot reloading work with Vue CLI 3?

No, not yet. I tried creating a new Vue project through the CLI 3 and hot reloading works there. I'm trying to figure out the differences. I faced the same issue (the console showed an error that said " [WDS] Disconnected") and here's what I did. Note that I was using the Vue UI tool for managing my project and I did not edit any config files.

Can Vue config file handle Apache configuration errors?

Last night when I was messing around with my Apache configurations I found out that this error could be handled simply by Vue config file provided by Vue CLI 3. If you are struggling with the same problem, you are reading the right article. To understand the problem and the solution better let's talk a little bit about the error.

What are the common errors with Sass and Vue?

Error with sass and Vue 3 - Cannot find module 'sass' 3 Unable to use swiper/vue dependencies not found 2 node-gyp build error while running npm install in reactjs


1 Answers

using FS module while bootstrapping the app

You can use fs when bootstrapping the application. For instance you could read a file in your file system to grab something, then add it to the application to use it somehow. If this is what you want (I doubt), you can just setup something like the following in your vue.config.js:

const fs = require('fs');

const someFileContents = fs.readFileSync('my-path-to-the-file');

module.exports = {
  lintOnSave: true,

  configureWebpack: config => {
    return {
      plugins: [
        new webpack.DefinePlugin({
          'somevar': someFileContents,
        })
      ]
    }
  },
}

And now, whenever you write somevar in your web app code, webpack will had it replaced -at compile time- with the contents of the file. This may have some uses I think, but never did it.

Using FS module in browser

Due to security reasons, you cannot use the fs module in the browser: you won't be able to setup a Vue component that lists filesystem items or write to disk. It is not a webpack, node nor Vue issue, it is due to security limitations of browsers. The native fs module of node.js talks to OS low level API's, and the browser can't access them.

should javascript be able to modify or read filesystems, a lot of devastating pretty bad security screw ups will arise.

The FileSystem API

Now, there is this pretty new browser API, the FileSystem API.

It does not allow to access user filesystem, yet it somehow simulates a sandboxed folder where your web application can store files. It may be useful for you depending on your use case, but does not have the greatest browser support.

 Webpack configuration

There is no webpack config file anywhere.

In fact, all your webpack stuff in a vue-cli 3 app should be done via the vue.config.js file: https://cli.vuejs.org/guide/webpack.html.

There you can execute arbitrary node.js code (in fact you can call fs from there, as said before, since it is code running on your machine, not in the browser).

like image 158
Sergeon Avatar answered Oct 03 '22 02:10

Sergeon