How to use NPM package in Vue if it requires Nodej

2019-08-21 03:57发布

Every time I attempt to use an NPM package in Vue that requires fs it doesn't work and it errors out. Normally along the lines of fs could not be resolved or fs.readFileSync is not a function.

I have seen some other pages that say fs doesn't work in browsers because of permission etc...

Just to add, I am not explicitly trying to use the fs module. I am just trying to use the npm package which happens to use fs.

What should I do to make it work? Ive tested these packages on the npm runkit, and everything is fine. And that runs in a browser, so I'm a little confused there also.

1条回答
爷的心禁止访问
2楼-- · 2019-08-21 04:48

The problem here is that Vue will be rendered client side. fs is just for node applications, because it is server side (you should not be able to read data from a client file system). You you have two options:

  1. Not using the module
  2. Or, if you do not need the fs part and the module is under a open source
    license (which the module you mentioned in comments is): copying the module and removing the fs part
查看更多
登录 后发表回答