简体   繁体   English

如何在 React 中使用需要文件系统和路径的包

[英]How to use packages that require fs & path with React

I need to use quick.db package in my ReactAPP for hook, but React don't let use FS & Path, that is require for package我需要在我的 ReactAPP 中使用quick.db package 作为挂钩,但 React 不允许使用 FS 和路径,这是 package 所必需的

I got this errors:我得到这个错误:

ERROR in ./node_modules/file-uri-to-path/index.js 5:10-29
Module not found: Error: Can't resolve 'path' in 'C:\\Users\\Moruga\\projects\\character-manager-jjk-rp\\node_modules\\file-uri-to-path'
ERROR in ./node_modules/bindings/bindings.js 5:9-22
Module not found: Error: Can't resolve 'fs' in 'C:\\Users\\Moruga\\projects\\character-manager-jjk-rp\\node_modules\\bindings'

I already try to write我已经尝试写

"browser": {
"fs": false,
"path": false,
"os": false
},

in package.json but it don't helppackage.json但它没有帮助

You cannot run all npm packages everywhere.你不能到处运行所有 npm 包。

You should use node.js javascript packages only on a server, it does not run in the browser.你应该只在服务器上使用 node.js javascript 包,它不会在浏览器中运行。 Those packages you want to run are supposed to run only on a server (or your local machine), not in the browser.您要运行的那些包应该只在服务器(或您的本地计算机)上运行,而不是在浏览器中运行。 They are supposed to have access to thinks like files,.network, databases, etc.他们应该可以访问文件、网络、数据库等。

For this particular reason you cannot utilise them with React.由于这个特殊原因,您不能将它们与 React 一起使用。

React app is a client side javascript package, meaning it's supposed to run in a browser without an access to the OS stuff, like files, databases, etc. React 应用程序是一个客户端 javascript package,这意味着它应该在浏览器中运行而无需访问操作系统的东西,如文件、数据库等。

For React app to access DB, files,.network or whatever, you need to have 2 separate projects.要让 React 应用程序访问数据库、文件、.network 或其他任何内容,您需要有 2 个独立的项目。 One that's running node.js (purely on server, or in terminal) and 2nd running React app.一个运行 node.js(完全在服务器或终端中)和第二个运行的 React 应用程序。 They would communicate using GET/POST.network calls他们将使用 GET/POST.network 调用进行通信

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM