简体   繁体   English

react-native-twitter-lite:错误:无法解析模块“加密”

[英]react-native-twitter-lite: Error: Unable to resolve module 'crypto'

I have followed the instructions to install react-native-twitter-lite via npm page ....as per below:我已按照说明通过npm 页面安装 react-native-twitter-lite ....如下所示:

npm install twitter-lite

....then on app.js: ....然后在 app.js 上:

import Twitter from 'twitter-lite';

const twtClient = new Twitter({
  consumer_key: 'xxxx',
  consumer_secret: 'xxxx',
});

...I get error Unable to resolve module crypto from node_modules\twitter-lite\dist\twitter.js: crypto could not be found within the project ...even though I can see that the file node_modules\twitter-lite\dist\twitter.js exists ...我收到错误Unable to resolve module crypto from node_modules\twitter-lite\dist\twitter.js: crypto could not be found within the project ...即使我可以看到文件node_modules\twitter-lite\dist\twitter.js存在

The issue might be that cryptojs is missing from your dependencies (not installed in your app), but react-native-twitter-lite still need it to run for security.问题可能是您的依赖项中缺少 cryptojs(未安装在您的应用程序中),但 react-native-twitter-lite 仍然需要它来运行以确保安全。 You surely have the twitter.js but not the crypto.js.你肯定有 twitter.js 但没有 crypto.js。 Cryptojs is used by twitter-lite to cypher and encrypt data. Cryptojs 被 twitter-lite 用于 cypher 并加密数据。 You may choose to use Nodejs's crypto module or react-native's build-in cryptojs.你可以选择使用 Nodejs 的 crypto 模块或 react-native 的内置 cryptojs。 I always choose the later one for a more native behavior.我总是选择后者以获得更原生的行为。 For that, simple run为此,简单运行

npm install react-native-crypto-js --save. npm 安装 react-native-crypto-js --save。

The first time I run into this issue this source helped me understand.我第一次遇到这个问题时,这个来源帮助我理解了。

This happens to me, most likely when installing a new package to my project, I didn't try to install react-native-twitter-lite , but my general approach is:这发生在我身上,很可能在我的项目中安装新的 package 时,我没有尝试安装react-native-twitter-lite ,但我的一般方法是:

  • Try to install the missing module yourself, in your case, install Crypto尝试自己安装缺少的模块,在您的情况下,安装 Crypto
  • Delete node_modules and install the dependencies again, run rm -rf node_modules && npm install删除node_modules重新安装依赖,运行rm -rf node_modules && npm install
  • run your project npm start and then npm run android && npm run ios run your project npm start and then npm run android && npm run ios

If you install native module you will need to rebuild your app.如果您安装本机模块,您将需要重新构建您的应用程序。 It's jut hot reloadable.它只是热重载。

If that's not your issue, but you're using typescript, you may be missing a dependency that can fortunately be resolved using:如果这不是您的问题,但您使用的是 typescript,那么您可能会缺少一个可以使用以下方法解决的依赖项:

npx typesync npx 类型同步

Are you using Expo or Xcode/Android Studio?您使用的是 Expo 还是 Xcode/Android Studio? If Expo, note that Expo handles a Bare and a Managed workflow.如果 Expo,请注意 Expo 处理裸工作流和托管工作流。 If you are using a managed workflow you may have these issues since Expo is managing native modules for you so you may want to check if Expo has a similar dependency to what you're using, otherwise you may want to eject from Expo.如果您使用的是托管工作流,您可能会遇到这些问题,因为 Expo 正在为您管理本机模块,因此您可能需要检查 Expo 是否与您正在使用的内容具有相似的依赖性,否则您可能需要从 ZC29F23F279126757BA18EC74 中弹出

If developing for iOS, you may also want to check if you've installed the pods you need.如果为 iOS 开发,您可能还需要检查是否已安装所需的 pod。 You can do this using the following:您可以使用以下方法执行此操作:

npx pod-install from root of project directory (likely same directory as package.json) npx pod-install 从项目目录的根目录(可能与 package.json 相同的目录)

All else fails: rn -rf node_modules npm install && npx pod-install (If usingTypescript also try npx typesync)一切都失败了: rn -rf node_modules npm install && npx pod-install (如果使用Typescript也尝试npx typesync)

If developing for Android, make sure you add necessary code on gradle file, can be pasted anywhere (likely in docs)如果为 Android 开发,请确保在 gradle 文件中添加必要的代码,可以粘贴到任何地方(可能在文档中)

Have a look in the issues on repo查看 repo 上的问题

Crypto-js dependency Issue Crypto-js 依赖问题

Hope you might find the right way to figure out your issue.希望您能找到正确的方法来解决您的问题。

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

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