Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 学习节点-找不到库_Node.js - Fatal编程技术网

Node.js 学习节点-找不到库

Node.js 学习节点-找不到库,node.js,Node.js,我正在尝试使用库获取运行示例的节点: 安装后,我通过节点repl运行代码(我通过从终端运行“node”启动repl): 但接收错误: ReferenceError: bitcoin is not defined at repl:1:15 at REPLServer.defaultEval (repl.js:262:27) at bound (domain.js:287:14) at REPLServer.runBound [as eval] (domain.js

我正在尝试使用库获取运行示例的节点:

安装后,我通过节点repl运行代码(我通过从终端运行“node”启动repl):

但接收错误:

ReferenceError: bitcoin is not defined
    at repl:1:15
    at REPLServer.defaultEval (repl.js:262:27)
    at bound (domain.js:287:14)
    at REPLServer.runBound [as eval] (domain.js:300:12)
    at REPLServer.<anonymous> (repl.js:431:12)
    at emitOne (events.js:82:20)
    at REPLServer.emit (events.js:169:7)
    at REPLServer.Interface._onLine (readline.js:211:10)
    at REPLServer.Interface._line (readline.js:550:8)
    at REPLServer.Interface._ttyWrite (readline.js:827:14)
ReferenceError:未定义比特币
回复:1:15
在REPLServer.defaultEval(repl.js:262:27)
绑定时(domain.js:287:14)
在REPLServer.runBound[as eval](domain.js:300:12)
在服务器上。(回复js:431:12)
在emitOne(events.js:82:20)
在REPLServer.emit上(events.js:169:7)
在REPLServer.Interface.\u在线(readline.js:211:10)
在REPLServer.Interface.\u行(readline.js:550:8)
在REPLServer.Interface.\u ttyWrite(readline.js:827:14)

在node repl中运行之前是否需要比特币库?

是的,在NodeJS中,您需要将所有模块作为变量导入才能使用它们。在您的实例中,可以执行以下操作:

var bitcoin = require('bitcoinjs-lib');
...
var keyPair = bitcoin.ECPair.makeRandom()

编辑::快速澄清“将所有模块作为变量导入”的含义:NodeJS有一个简单的模块加载系统。“require”语句可以从全局或本地包导入,也可以从相对路径导入。它或多或少地告诉节点运行时使用语句提供的名称查找JavaScript文件。在REPL中还需要注意的一点是,如果某个模块没有作为全局模块安装,并且您所在的目录中没有包含作为依赖项的“bitcoinjs lib”包,那么您将无法要求它

是的,在NodeJS中,在使用模块之前,您需要将所有模块作为变量导入。在您的实例中,可以执行以下操作:

var bitcoin = require('bitcoinjs-lib');
...
var keyPair = bitcoin.ECPair.makeRandom()

编辑::快速澄清“将所有模块作为变量导入”的含义:NodeJS有一个简单的模块加载系统。“require”语句可以从全局或本地包导入,也可以从相对路径导入。它或多或少地告诉节点运行时使用语句提供的名称查找JavaScript文件。在REPL中还需要注意的一点是,如果某个模块没有作为全局模块安装,并且您所在的目录中没有包含作为依赖项的“bitcoinjs lib”包,那么您将无法要求它

您如何知道所需的图书馆名称?通过查看已下载库的名称?请参阅库页面的“入门”部分。在这种情况下,您如何知道所需库的名称?通过查看已下载库的名称?请参阅库页面的“入门”部分。在这种情况下,请执行
npm安装-g比特币库