bitcoinjs / ecpair

The ECPair module for bitcoinjs-lib

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

i have get SyntaxError: Unexpected token error

arlong87 opened this issue · comments

/root/node_modules/ecpair/src/ecpair.js:17
__D;
^

SyntaxError: Unexpected token ;
i have get an error like this.
my npm v is6
node version is 10.

<v10, v10, v11, v13, and v15 are end-of-life.

Please use node v12, v14, v16 or >v16

i try v12 and v16 still give me same error.

create_btc_address.js

'use strict';

module.exports = function create_btc_address(){
	const  bitcoin =require('bitcoinjs-lib');

	const {  Signer, SignerAsync, ECPairInterface, ECPair } = require('ecpair');

	const TESTNET = bitcoin.networks.testnet;

	const keyPair = ECPair.makeRandom({ network: TESTNET });
	const { address } = bitcoin.payments.p2pkh({
	  pubkey: keyPair.publicKey,
	  network: TESTNET,
	});
	const privateKey = keyPair.toWIF();
	console.log(address+" "+privateKey);
}

app.js

const create_btc_address= require('./modules/btc/create_btc_address');
setInterval( function() { create_btc_address() }, 3000);

Following in a bash terminal worked. nvm use v12 using the latest v12 node version: v12.22.7

cd $(mktemp -d)

nvm use v12

npm init -y
npm install bitcoinjs-lib ecpair

cat > create_btc_address.js <<'EOF'
'use strict';

module.exports = function create_btc_address(){
        const  bitcoin =require('bitcoinjs-lib');

        const {  Signer, SignerAsync, ECPairInterface, ECPair } = require('ecpair');

        const TESTNET = bitcoin.networks.testnet;

        const keyPair = ECPair.makeRandom({ network: TESTNET });
        const { address } = bitcoin.payments.p2pkh({
          pubkey: keyPair.publicKey,
          network: TESTNET,
        });
        const privateKey = keyPair.toWIF();
        console.log(address+" "+privateKey);
}
EOF

cat > app.js <<'EOF'
const create_btc_address= require('./create_btc_address');
setInterval( function() { create_btc_address() }, 3000);
EOF

node app.js
# n3UEC5E28BLy9cLy19wKLhPiiFekvLmpdb cMpAHcL3chSHxfKkXpXr2ybiUPPtkBo2qE8otpFquwvokzrNL2dG
# n18bxZ5ioTB2oouaGKs9XGCiu1qBBgK3G6 cQQectEQtgjQZTqxqYcjJwswyE2fHyP5gfJhTxoSmvG48QWTkiCX
# msvoPGo9962iMiG5K3XftvJLvgZejjh3u4 cPk2yn2JEThpb2mfLH9SYuFCFoGiiuQcMUPjrY5GcwCpMXXmi5QE
# ...

Also note that Signer, SignerAsync, ECPairInterface are TypeScript interfaces, so if you do this in JavaScript, those will all be undefined

Try running node -v and checking the node version before you run node app.js

sorry i tried all these but i didn't succeed maybe there is a problem with my system. thank you.

Can we relook at this issue? I am having a similar problem when trying to use this library in an app created using create-react-app.

I get:

Failed to compile.

./node_modules/ecpair/src/ecpair.js 80:7
Module parse failed: Unexpected token (80:7)
File was processed with these loaders:
 * ./node_modules/babel-loader/lib/index.js
You may need an additional loader to handle the result of these loaders.
|
|   class ECPair {
>     __D;
|     __Q;
|     compressed;

I believe it has to do with create-react-app@4.0.3 not including @babel/plugin-proposal-class-properties plugin.

It can be fixed by updating to create-react-app@5.0.0 but then that has a host of other issues I'm running into (facebook/create-react-app#11756).

I am able to resolve the issue by compiling the typescript to "target": "es6" instead of "target": "ESNEXT"` and then using the package.

I can confirm this problem still happens.

I am having this issue when running Cypress e2e tests in CRA 5 project.