我正在尝试安装 histogramjs 包。 npm i histogramjs
--->效果很好,但是当我尝试运行时 --> var hist = require('histogramjs')
我在 cmd 上收到错误:
'var' is not defined as internal or external command in npm
我已经尝试过了
npm install var
-->当我尝试运行时安装 abd ---> var env = require('var');
得到同样的错误..
如果直接从node.js运行,会出现错误'cannot find module 'var'
请帮忙,因为我对 npm 和 node 很陌生 谢谢
最佳答案
您不应在命令行中输入 var hist ....
。您在那里输入的内容将由 shell 执行,而不是由 npm/node 执行。
相反,请尝试:
- 运行
npm init
。按 Enter 几次,直到完成。这将创建 package.json 文件,它是 npm 的配置文件。该文件将告诉 npm 以及其他文件,哪个文件是您的应用程序的入口点。 - 编辑
package.json
,在“scripts”
元素中添加“start”:“node app.js”,
(请参见下面的示例)。 - 在
package.json
所在的同一目录中创建app.js
。将您的脚本放入此文件中。 - 运行
npm start
。
如果您的脚本依赖于第三方模块(在您的情况下 - 可能是“histogramjs”),也请安装这些模块:
npm install --save histogramjs
在没有 --save
修饰符的情况下运行它也可以。但是 --save
会导致 npm 将“histogramjs”作为 package.json 中的依赖项。因此,当有人稍后获取您的代码(即从存储库)时,他将能够简单地运行 npm install
,甚至不必关心需要哪些依赖项。
package.json
{
"name": "t",
"version": "0.0.0",
"description": "",
"main": "app.js",
"scripts": {
"start": "node app.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "BSD"
}
app.js
var hist = require('histogramjs')
// ... rest of your code
关于javascript - 'var' 未在 npm 中定义为内部或外部命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22863261/