Node.js Const未被识别为cmdlet的名称
我试图通过一个构建在Node上的API获取数据(第一次使用Node) 这是我根据文档编写的代码: 但是,我不断遇到以下错误消息:Node.js Const未被识别为cmdlet的名称,node.js,Node.js,我试图通过一个构建在Node上的API获取数据(第一次使用Node) 这是我根据文档编写的代码: 但是,我不断遇到以下错误消息: const : The term 'const' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that
const : The term 'const' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ const boxrec = require("boxrec").Boxrec;
+ ~~~~~
+ CategoryInfo : ObjectNotFound: (const:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
将终端或命令行用于诸如
npm install{package name}
之类的命令-g
标志仅在您希望它被安装并可用于多个项目或作为命令行功能时使用
在您的情况下,只需使用C:\>boxrec
即可立即运行该工具,您可能需要额外的参数来使用它
并非每个npm包都是命令行工具有些只是帮助您开发的JavaScript库
一旦安装了包,就可以在JavaSript文件的开发中使用它,运行以下命令
const boxrec = require("boxrec").Boxrec;
// now boxrec can be used
const cookieJar = await boxrec.login(BOXREC_USERNAME, BOXREC_PASSWORD);
var person = await boxrec.getPersonById(cookieJar, 352);
要运行代码,您需要运行
node login.js
,您是否要在命令行中运行此命令npm install boxrec-g
将在系统的全局回购中安装此软件包const-boxrec=required(“boxrec”)。如果boxrec
位于类似index.js
的文件中,则可以使用该命令运行node index.js
line@Andrei我试图运行命令行中的每一行。为了清楚起见,您是说在文件中保存constboxrec,然后在命令行中运行node index.js?尝试在命令行npm install boxrec-g
中抛出模块未找到错误消息是一个npm命令,您可以在bash终端中运行它<代码>常量boxrec=要求(“boxrec”).boxrec是JavaScript,您可以使用Node shell运行它(您可以使用命令Node
)或在JS文件中启动Node shell。该错误消息看起来像PowerShell错误-您需要在Node.JS环境中运行代码,正如所建议的那样。@Emm这里有一个关于本地和-g
标志之间区别的快速阅读。强烈建议使用本地而不是全局。相反,您应该在项目目录中执行npm install boxrec
,这样您就可以在index.js
中引用它,因为在写下此代码后,我在我的powershell中运行node index.js,并收到以下错误消息:“const cookieJar=wait boxrec.login('xx',xxx');^^^SyntaxError:await只在异步函数中有效,“是的,现在进入JavaScript。您需要了解承诺
和异步/等待
模式。您可以将使用wait
的两行代码包装到异步函数调用中。示例异步函数loginUser(){//code with await}
然后调用函数执行loginUser()
。这有点超出了你问题的原始范围
const boxrec = require("boxrec").Boxrec;
// now boxrec can be used
const cookieJar = await boxrec.login(BOXREC_USERNAME, BOXREC_PASSWORD);
var person = await boxrec.getPersonById(cookieJar, 352);