反应原生的节点模块

时间:2017-03-13 16:05:08

标签: javascript node.js reactjs react-native react-native-android

我刚尝试将一些节点模块添加到react-native中。 我发现它只适用于react-native modulesreact-native link模块。

我想用: - https://www.npmjs.com/package/node-google-image-search 要么 - https://www.npmjs.com/package/first-image-search-load

我试过了: Link1 Link2 Link3 Link4

当我使用命令链接“非反应模块”时,我仍然得不到任何结果: enter image description here

此外,当我尝试通过以下方式添加此模块时:

  1. import 'first-image-search-load'
  2. import {saveFirstImage} from 'first-image-search-load'
  3. import * from 'first-image-search-load'
  4. var first-image-search-load = require('first-image-search-load');
  5. var first-image-search-load = require('./first-image-search-load');
  6. var first-image-search-load = require('./node-modules/first-image-search-load');
  7. 并尝试将这些放入index.android.js我得到: enter image description here

      

    开发服务器返回响应错误代码:500

         

    URL:   http://10.0.2.2:8081/index.android.bundle?platform=android&dev=true&hot=false&minify=false

         

    正文:{“message”:“无法解析模块https   /Users/Pien/Projects/smog_bielsko/node_modules/node-google-image-search/index.js:   模块映射或这些目录中不存在模块:\ n   /用户/片仔癀/项目/ smog_bielsko / node_modules /节点谷歌图像搜索/ node_modules \ N,   / Users / Pien / Projects / smog_bielsko / node_modules \ n \ n这可能是   与https://github.com/facebook/react-native/issues/4968 \ n相关   请尝试以下操作:\ n 1.清除守望者监视:watchman watch-del-all。\ n 2.删除node_modules文件夹:rm -rf node_modules && npm install。\ n 3.重置打包程序缓存:{{1} }或rm -fr $TMPDIR/react-*。“,”name“:”UnableToResolveError“,”type“:”UnableToResolveError“,”errors“:[{}]}   onResponse       DevServerHelper.java:323执行       RealCall.java:126运行       NamedRunnable.java:32 runWorker       ThreadPoolExecutor.java:1113运行       ThreadPoolExecutor.java:588运行       Thread.java:818

    请告诉我如何将正常的npm插件导入react-native ..

    THX

3 个答案:

答案 0 :(得分:1)

实际上,ReactNativify项目应该这样做,让我们在React-Native代码项目(RN)中使用NodeJS模块。

在我写这篇文章时,2017年5月,project seems to not work with RN versions 0.43.3+, due to breaking changes

答案 1 :(得分:1)

Big Rich提供的有关答案的一些其他信息。

我不认为您使用ReactNativify填充方法可以解决您遇到的错误。否则,错误会提到缺少的Node API对象。

但是如果你的first-image-search-node包是为Node编写的,并且想要使用ReactNativify,那么你应该注意一些其他问题,我在这里记录了这些:< / p>

Can we use nodejs code inside react native application?

简而言之,它强调:

  1. 将变压器拆分为2个文件的方法。
  2. 建议使用&#39; babel-plugin-module-resolver&#39;
  3. .babelrc中写下所有垫片替换,而不是js code
  4. 即使您有.babelrc
  5. ,也应包含根transformer.js

答案 2 :(得分:0)

您无需链接不使用本机代码的软件包。对于只有js的库,您需要做的只是npm install packageName。如果该库具有任何html / css / dom内容,则它将无法与react native一起使用。

相关问题