Browser vs Node.js Usage
Node.js Setup
For Node.js environments, OP_NET libraries requires version 24.0.0 or higher.
If required, download the latest version from the official Node.js website.
Use the following command to check your current Node.js version:
node --version # Should be >= v24.0.0Basic Project Structure
A basic Node.js project should follow this folder structure:
package.json Configuration
The minimal package.json file should contain the following configuration:
{
"name": "my-opnet-app",
"type": "module",
"scripts": {
"build": "tsc",
"start": "node build/index.js"
},
"dependencies": {
"opnet": "^1.7.35",
"@btc-vision/transaction": "^1.7.31",
"@btc-vision/bitcoin": "^6.5.4"
},
"devDependencies": {
"typescript": "^5.9.2"
}
}Versions listed were current at the time of writing. Update to the latest versions as needed.
Verify Installation
Create a basic test script in src/index.ts.
import { JSONRpcProvider, version } from 'opnet';
import { networks } from '@btc-vision/bitcoin';
console.log('OPNet version:', version);
const provider = new JSONRpcProvider(
'https://regtest.opnet.org',
networks.regtest
);
async function main() {
const blockNumber = await provider.getBlockNumber();
console.log('Current block:', blockNumber);
}
main().catch(console.error);Then run this nmp command:
npm run build && npm startTroubleshooting
Module Resolution Issues
Ensure your package.json has "type": "module".
{
...
"type": "module"
...
}TypeScript Import Errors
If TypeScript cannot resolve module types, verify the following settings in your tsconfig.json:
{
...
"compilerOptions": {
"moduleResolution": "NodeNext",
"esModuleInterop": true
}
...
}Browser Setup
For browser environments, OP_NET libraries require additional bundler configuration. While Webpack is supported, Vite is the recommended bundler for OP_NET browser applications.
Using Vite (recommended)
Install the following packages:
npm install vite vite-plugin-node-polyfills --save-devCreate a vite.config.ts file with the following configuration:
import { defineConfig } from 'vite';
import { nodePolyfills } from 'vite-plugin-node-polyfills';
export default defineConfig({
plugins: [
nodePolyfills({
include: ['buffer', 'crypto', 'stream'],
}),
],
resolve: {
alias: {
buffer: 'buffer',
},
},
define: {
global: 'globalThis',
},
});Using Webpack
Install the following packages:
npm install webpack webpack-cli buffer stream-browserify --save-devCreate a webpack.config.ts file with the following configuration:
const webpack = require('webpack');
module.exports = {
resolve: {
fallback: {
buffer: require.resolve('buffer/'),
stream: require.resolve('stream-browserify'),
crypto: false,
},
},
plugins: [
new webpack.ProvidePlugin({
Buffer: ['buffer', 'Buffer'],
}),
],
};Browser Import
Ensure these polyfills are loaded first:
// Import polyfills before opnet
import 'opnet/browser/polyfills';
// Then import opnet
import { JSONRpcProvider } from 'opnet';Import from the browser-specific path:
// Automatic (recommended) - bundler resolves correct path
import { JSONRpcProvider, getContract } from 'opnet';
// Explicit browser import
import { JSONRpcProvider, getContract } from 'opnet/browser';