node:internal/modules/cjs/loader:936
I'm using VSCode on p2pkh.js but it seems that the code content itself requires Identification and Function, which is why the code p2pkh.js becomes like this below:
const { forgeTx } =require('txforge')
// We'll use these Casts in our transaction const casts = () => { P2PKH, OpReturn }
// You'll need UTXOs to fund a transaction. Use the toUTXO helper to turn
// your UTXO data into the required objects.
const utxo = () => toUTXO({
txid, // utxo transaction id
vout, // utxo output index
satoshis, // utxo amount
script // utxo lock script
})
// Forge a transaction const P2PKH = () => { P2PKH } const myPrivateKey = () => { myPrivateKey }
const tx = forgeTx({
inputs: [
function unlock() {
P2PKH.unlock(utxo, { privkey: myPrivateKey })
}
],
outputs: [
function lock() {
P2PKH.lock(5000, { address: '1DBz6V6CmvjZTvfjvWpvvwuM1X7GkRmWEq' }),
OpReturn.lock(0, { data: ['meta', '1DBz6V6CmvjZTvfjvWpvvwuM1X7GkRmWEq', txid] })
}
],
change: { address: '1Nro9WkpaKm9axmcfPVp79dAJU1Gx7VmMZ' }
})
// And behold! Forged by the Gods and found by a King - a transaction is born. console.log(tx.toHex())
- When I "Run" the above the error prompted as:
node:internal/modules/cjs/loader:936 throw err; ^
Error: Cannot find module '/Users/jsjs77/node_modules/@runonbitcoin/nimble/classes/p2pkhs.js' at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15) at Function.Module._load (node:internal/modules/cjs/loader:778:27) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12) at node:internal/main/run_main_module:17:47 { code: 'MODULE_NOT_FOUND', requireStack: []
Any solution for the above? Note that my VScode in Mac unable to Import, that's why use "const"
Thanks
The casts object is exported from the main module. So you use it like this:
const { forgeTx, casts } =require('txforge')
// Get the casts you will use in your transaction
const { P2PKH, OpReturn } = casts
Thanks for getting back to me along with the solution. I have amended and right now, the new error as the following:
Error: P2PKH unlock must be created with valid 'privkey' as per the screenshot.

Then, I did input the "privkey" from my wallet (saved) and to Run again but the Error message still the same as per the screenshot below:

Does this mean I need to create 'privkey' in a separate coding?
Please advice, thanks so much.