[英]Why my JavaScript file can't access defined constant from another file?
I'm going to embed data into the blockchain with OP_RETURN (testnet).我将使用 OP_RETURN (testnet) 将数据嵌入到区块链中。
I have two files in one directory.我在一个目录中有两个文件。 The first one
keys.js
contains code that generates address and private key for bitcoin testnet transactions.第一个
keys.js
包含为比特币测试网交易生成地址和私钥的代码。
keys.js:键.js:
const bitcoin = require('bitcoinjs-lib');
const { testnet } = bitcoin.networks
const myKeyPair = bitcoin.ECPair.makeRandom({ network: testnet });
//extract the publickey
const publicKey = myKeyPair.publicKey;
//get the private key
const myWIF = myKeyPair.toWIF();
//get an address from the myKeyPair we generated above.
const { address } = bitcoin.payments.p2pkh({
pubkey: publicKey,
network: testnet
});
console.log("myAdress: " + address + " \nmyWIF: " + myWIF);
The second one op_return.js
contains method that allows me to embed random text into blockchain.第二个
op_return.js
包含允许我将随机文本嵌入区块链的方法。
This is the end of op_return.js:这是 op_return.js 的结尾:
const importantMessage = 'RANDOM TEXT INTO BLOCKCHAIN'
buildOpReturnTransaction(myKeyPair, importantMessage)
.then(pushTransaction)
.then(response => console.log(response.data))
The problem is with constant myKeyPair
in op_return.js
because after typing node op_return
in node.js command prompt error comes out:问题在于 op_return.js 中的常量
myKeyPair
,因为在op_return.js
命令提示符错误中键入node op_return
后:
buildOpReturnTransaction(myKeyPair, importantMessage)
^
ReferenceError: myKeyPair is not defined
at Object.<anonymous> (C:\Users\Paul\Desktop\mydir\op_return:71:26)
at Module._compile (internal/modules/cjs/loader.js:1133:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1153:10)
at Module.load (internal/modules/cjs/loader.js:977:32)
at Function.Module._load (internal/modules/cjs/loader.js:877:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)
at internal/main/run_main_module.js:18:47
A variable declared in one JavaScript file is not automatically accessible in a different file, but there is a feature available in Node.js that lets you import and export variables via modules .在 JavaScript 文件中声明的变量不能在其他文件中自动访问,但 Node.js 中有一项可用功能,可让您通过模块导入和导出变量。
Say you have defined the variable myKeyPair
in 'file1.js', but you want to use myKeyPair
in 'file2.js'.假设您在“file1.js”中定义了变量
myKeyPair
,但您想在“file2.js”中使用myKeyPair
。
The solution is to export myKeyPair
in file1.js:解决办法是在file1.js中导出
myKeyPair
:
// file1.js
const myKeyPair = ['hello', 'world'];
module.exports.myKeyPair = myKeyPair;
Then, to use myKeyPair
in file2.js, you import it from file1.js with a require()
statement.然后,要在 file2.js 中使用
myKeyPair
,请使用require()
语句从 file1.js 导入它。
// file2.js
const myKeyPair = require('./file1.js');
You have defined myKeyPair
in keys.js
and not in op_return.js
.您在
myKeyPair
中定义了keys.js
而不是在op_return.js
中。 If you need to define it in one file and use it in another, you need to define the variable as a global.如果您需要在一个文件中定义它并在另一个文件中使用它,则需要将变量定义为全局变量。 Checkout the link below for global variables in node
查看下面的链接以获取节点中的全局变量
https://stackabuse.com/using-global-variables-in-node-js/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.