简体   繁体   中英

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.

From command line:

npm install sjcl --save
react-native link

In a RN JS file:

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/. I also need sha1.js from node_modules/sjcl/core/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?

I had the same problem trying to load the ECC module, here is how I worked around it (I installed sjcl using npm). Create a file LocalExports.js in which put this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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