Ripple-Lib utilizes specific dependencies that rely on node.js modules accessed through require
or directly from the global
context. However, the NativeScript environment differs from both node.js and browsers, requiring us to ensure all dependencies are met for {N} execution.
In many scenarios, the nativescript-nodeify plugin suffices. Nevertheless, when working with Ripple-Lib
, this plugin fails to address compatibility issues, necessitating manual intervention:
1) The dependency of Ripple-Lib
, Bignumber.js, relies on the native node library crypto. Since this is unavailable in the {N} runtime, we must utilize a specially designed module like nativescript-randombytes and make it globally accessible using Webpack's providePlugin:
Integrate the NativeScript plugin into the project:
tns plugin add nativescript-randombytes
Create a file as a partial implementation of the crypto
module:
// Example path: project/app/shims/crypto.js
module.exports.randomBytes = require('nativescript-randombytes')
Add it to the plugins configuration in webpack.config.js
:
plugins: [
..., // other plugins
new webpack.ProvidePlugin({
crypto: resolve(__dirname, 'app/shims/crypto.js')
})
]
Add an resolve.alias
for our version of crypto
in the webpack.config.js
to allow child dependencies to utilize our crypto
implementation:
alias: {
..., // Other aliases
'crypto': resolve(__dirname, 'app/shims/crypto.js')
}
2) Although some required modules may be missing, they can still be installed manually since they are compatible with the NativeScript runtime:
npm i -S lodash bufferutil tls utf-8-validate
3) As Ripple-Lib
does not function with standard websockets implementations, opting for a cross-platform solution like nativescript-websockets is necessary.
Add the plugin to the project:
tns plugin add nativescript-websockets
Import it before importing Ripple-Lib
:
import 'nativescript-websockets'
import { RippleAPI } from 'ripple-lib'
4) The net
node.js module is also essential for establishing connections, which can be emulated using Webpack's node mocks. Include the following under the node
config options in the webpack.config.js
:
node: {
..., // Other default NativeScript shims
"net": 'mock',
},
5) Some libraries may default to Node environment, requesting non-existent modules. To rectify this, set resolve.aliasFields to 'browser' as shown below:
resolve: {
// other options, aliases, etc
aliasFields: ['browser']
}
6) Another dependent library, create-hash
, relies on the crypto
module. Fortunately, it has a browser build available for usage. Add another alias
under the resolve
options in the webpack.config.js
:
alias: {
..., // Other aliases
'create-hash': 'create-hash/browser'
}
7) Upon completing all steps, ensure to clean up unnecessary project files by following these instructions:
Delete the following folders:
rm -rf platforms/android # or ios
rm -rf hooks
rm -rf node_modules
Reinstall packages and add platforms:
npm i
tns platform add android # or ios