如何在React中使用Node.js模块

问题描述

我尝试创建一个在浏览器中转换文件的应用程序。例如,我在本地使用gltf-pipeline。现在,我正在用React构建Web应用程序。不幸的是,我无法导入gltf-pipeline。似乎fs-extra有一些问题。我也不能使用此模块。所以我的问题是如何能够在浏览器中使用nodejs模块?我已经尝试过浏览器了,但是那没用。

感谢您的帮助。

-

编辑:

如果我直接通过以下方式导入模块:

import gltfPipeline from 'gltf-pipeline'

我收到以下错误

TypeError: Cannot read property 'native' of undefined
./node_modules/gltf-pipeline/node_modules/fs-extra/lib/fs/index.js
// fs.realpath.native only available in Node v9.2+
> 107 | if (typeof fs.realpath.native === 'function') {
  108 |   exports.realpath.native = u(fs.realpath.native)
  109 | }

如果我使用browserify导入生成文件,则会收到以下消息:

./src/Components/Page/Models/Converter/gltfPipeline.js
  Line 2:99:         'reqqq' is not defined                                                 no-undef
  Line 2:398:        'reqqq' is not defined                                                 no-undef
  Line 41:1:         Expected an assignment or function call and instead saw an expression  no-unused-expressions
  Line 41:124:       'define' is not defined                                                no-undef
  Line 41:135:       'define' is not defined                                                no-undef
  Line 41:159:       Expected an assignment or function call and instead saw an expression  no-unused-expressions
  Line 41:251:       Unexpected use of 'self'                                               no-restricted-globals
  Line 41:260:       Unexpected use of 'self'                                               no-restricted-globals
  Line 41:415:       '_dereq_' is not defined                                               no-undef
  Line 41:715:       '_dereq_' is not defined                                               no-undef
  Line 5373:27:      Unexpected use of 'self'                                               no-restricted-globals
  Line 5373:50:      Unexpected use of 'self'                                               no-restricted-globals
  Line 5790:208:     Unexpected use of 'self'                                               no-restricted-globals
  Line 5790:232:     Unexpected use of 'self'                                               no-restricted-globals
  Line 5790:277:     Unexpected use of 'self'                                               no-restricted-globals
  Line 5790:286:     Unexpected use of 'self'                                               no-restricted-globals
  Line 5791:93:      Unexpected use of 'self'                                               no-restricted-globals
  Line 5791:116:     Unexpected use of 'self'                                               no-restricted-globals
  Line 5831:2808:    'read' is not defined                                                  no-undef
  Line 5831:2964:    'readbuffer' is not defined                                            no-undef
  Line 5831:2984:    'read' is not defined                                                  no-undef
  Line 5831:3091:    'scriptArgs' is not defined                                            no-undef
  Line 5831:3214:    'quit' is not defined                                                  no-undef
  Line 5831:3238:    Unexpected use of 'print'                                              no-restricted-globals
  Line 5831:3314:    Unexpected use of 'print'                                              no-restricted-globals
  Line 5831:3377:    'printErr' is not defined                                              no-undef
  Line 5831:3386:    Unexpected use of 'print'                                              no-restricted-globals
  Line 5831:3486:    Unexpected use of 'self'                                               no-restricted-globals
  Line 5835:16223:   Expected an assignment or function call and instead saw an expression  no-unused-expressions

...

我遵循了本指南:https://hackernoon.com/using-core-node-js-modules-in-react-native-apps-64acd4d07140生成js文件后,vs代码中已经显示了一些错误

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)