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:

bash
node --version  # Should be >= v24.0.0

Basic Project Structure

A basic Node.js project should follow this folder structure:

my-opnet-app/ src/ index.ts package.json tsconfig.json

package.json Configuration

The minimal package.json file should contain the following configuration:

package.jsonjson
{
    "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.

index.tstypescript
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:

bash
npm run build && npm start

Troubleshooting

Module Resolution Issues

Ensure your package.json has "type": "module".

package.jsonjson
{
    ...
    "type": "module"
    ...
}

TypeScript Import Errors

If TypeScript cannot resolve module types, verify the following settings in your tsconfig.json:

tsconfig.jsonjson
{
    ...
    "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:

bash
npm install vite vite-plugin-node-polyfills --save-dev

Create a vite.config.ts file with the following configuration:

vite.config.tstypescript
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:

bash
npm install webpack webpack-cli buffer stream-browserify --save-dev

Create a webpack.config.ts file with the following configuration:

webpack.config.tsjavascript
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:

typescript
// Import polyfills before opnet
import 'opnet/browser/polyfills';

// Then import opnet
import { JSONRpcProvider } from 'opnet';

Import from the browser-specific path:

typescript
// Automatic (recommended) - bundler resolves correct path
import { JSONRpcProvider, getContract } from 'opnet';

// Explicit browser import
import { JSONRpcProvider, getContract } from 'opnet/browser';