简体   繁体   English

如何将整个npm库导入React Native,而不仅仅是1个文件?

[英]How to import an entire npm library into React Native, not just 1 file?

I've been trying to import the sjcl library, which has many files, but I'm only able to import 1 file from it. 我一直在尝试导入sjcl库,它有很多文件,但我只能从中导入1个文件。

From command line: 从命令行:

npm install sjcl --save
react-native link

In a RN JS file: 在RN JS文件中:

import sjcl from 'sjcl';

Looks like this doesn't import everything in the sjcl node package, it only imports file sjcl.js from node_modules/sjcl/. 看起来这不会导入sjcl节点包中的所有内容,它只从node_modules / sjcl /导入文件sjcl.js。 I also need sha1.js from node_modules/sjcl/core/sha1.js. 我还需要来自node_modules / sjcl / core / sha1.js的sha1.js。 I've tried various ways of importing it, but nothing works. 我尝试了各种导入方法,但没有任何效果。

How can I import an entire npm library into a React Native project? 如何将整个npm库导入React Native项目?

I had the same problem trying to load the ECC module, here is how I worked around it (I installed sjcl using npm). 我有同样的问题试图加载ECC模块,这是我如何解决它(我使用npm安装sjcl)。 Create a file LocalExports.js in which put this 创建一个文件LocalExports.js,在其中放置此文件

import sjcl from 'sjcl';
export {default as sjcl} from 'sjcl';
window.sjcl = sjcl;

then import like this in any other file 然后在任何其他文件中导入

import {sjcl} from './LocalExports';
import 'sjcl/core/bn'; 
import 'sjcl/core/ecc'; 

*edit - I just found this link, which is the official way to do this https://github.com/bitwiseshiftleft/sjcl/wiki/Getting-Started *编辑 - 我刚刚找到这个链接,这是官方的方式来做到这一点https://github.com/bitwiseshiftleft/sjcl/wiki/Getting-Started

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

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