Package Exports
- react-native-randombytes-nodeify
- react-native-randombytes-nodeify/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (react-native-randombytes-nodeify) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
react-native-randombytes-nodeify
randombytes nodeify for react-native (metro bundler), react-native-randombytes wrapper
randombytes for dependencies on react-native runtime
What is this
// many packages use "randombytes"
var randomBytes = require('randombytes');
// and "react-native-randombytes" package support like this and similar API
import { randomBytes } from 'react-native-randombytes'
// or
var { randomBytes } = require('react-native-randombytes');
// install this package and set your project
// now, you can use randombytes same node syntax
var randomBytes = require('randombytes');Installation
- add package
npm install react-native-randombytes-nodeify
// or
yarn add react-native-randombytes-nodeify- install peerDependencies and babel-plugin
npm install react-native-randombytes
npm install --save-dev babel-plugin-module-resolver
// or
yarn add react-native-randombytes
yarn add --dev babel-plugin-module-resolver- add a babel plugin to babel.config.js
module.exports = {
presets: ['module:metro-react-native-babel-preset'],
plugins: [
[
require.resolve('babel-plugin-module-resolver'),
{
alias: {
randombytes: './node_modules/react-native-randombytes-nodeify',
},
},
],
],
};Etc.
- If you try importing 'randombytes' before installing this package, you should clear react-native cache
npm start -- --reset-cacheUsage
// linking steps
// "randombytes" => "react-native-randombytes-nodeify" => "react-native-randombytes"'s randomBytes
var randomBytes = require('randombytes');
// get 4 random bytes
const rand = randomBytes(4)
// asynchronous API
randomBytes(4, (err, bytes) => {
// bytes is a Buffer object(4 random bytes)
console.log(bytes.toString('hex'))
})