【问题标题】:npm install: check that specific application is installednpm install:检查是否安装了特定的应用程序
【发布时间】:2016-04-26 15:43:10
【问题描述】:

我正在尝试为 Angular 应用程序编写单元测试,现在正致力于描述单元测试所需的所有包,例如 karma、phantomjs 等。操作系统:Ubuntu 14。

问题是,如果系统中没有安装“libfontconfig”,Phantom.js 将无法工作 (bug description),据我了解,无法安装它:

npm install --save-dev libfontconfig

如何在用户尝试执行某些安装时向用户输出警告:

npm install

更新

我决定就我的案例提供更具体的细节。所以,我将使用 Karma 和 Phantom.js 来测试我的应用程序。以下是项目的所有 devDependencies:

"devDependencies": 
{
"grunt": "~0.4.5",
"grunt-contrib-less": "~1.1.0",
"grunt-contrib-watch": "~0.6.1",
"jit-grunt": "~0.9.1",
"angular-route": "~1.4.8",
"angular": "~1.4.8",
"moment": "~2.10.6",
"jquery": "~1.11.3",
"bootstrap": "~3.3.6",
"angularjs-datepicker": "~0.2.16",
"phantomjs": "~1.9.8",
"karma": "^0.12.16",
"karma-jasmine": "~0.1.0",
"karma-phantomjs-launcher": "~0.2.3"
}

当我尝试使用 Karma 运行单元测试时:

node node_modules/karma/bin/karma start test/karma.conf.js

我收到以下错误:

ERROR [phantomjs.launcher]: /vagrant/node_modules/phantomjs/lib/phantom/bin/phantomjs: error while loading shared libraries: libfontconfig.so.1: cannot open shared object file: No such file or directory

所以,我的问题是:

1) 我应该检查这个库的存在还是我可以在 README 中描述它?

2)如果我应该检查,我该怎么做?使用 bash 是个好主意吗?据我了解,在这种情况下它不适用于 Windows 机器。

【问题讨论】:

    标签: angularjs node.js npm


    【解决方案1】:

    您应该可以在 package.json 中使用 scripts 字段

    https://docs.npmjs.com/misc/scripts

    只需要创建一个 nodeJS 脚本,您将在安装后调用它来检查 libfontconfig

    【讨论】:

    • 谢谢你的回答,我知道了,但我还是不明白如何从nodeJS获取系统中安装的应用程序的信息?
    • 您需要在安装后字段中设置的脚本中执行与 stackoverflow.com/a/15303236 类似的操作。
    • 好的,我尝试了这种方法,但看起来我无法检查应用程序是否安装在操作系统中 - 我只能在此处检查 npm 模块。虽然“libfontconfig”不是 npm 模块。也许可以在这里使用一些 bash 脚本?
    • 啊,我明白了.. 是的,这可能是最好的。您可以使用 exec 并查看“which libfontconfig”的输出。
    • @Tamara 您实际上可以在 npm 脚本部分中使用任何 shell 命令。了解人们如何将脚本功能用作 build system,运行构建应用程序所需的任何命令。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-16
    • 2011-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-22
    相关资源
    最近更新 更多