npm - NPM 脚本条件不适用于 Windows,但适用于 Linux 和 Mac
问题描述
下面的 NPM 脚本在 Linux 和 MacOS 上运行良好,但在 Windows 上不运行。我试图寻找一些解决方案,但找不到一个!
"scripts": {
"install-dependencies": "if [ ! -d node_modules ]; then npm install && npx npm-install-peers; fi",
},
我得到的错误是:
> if [ -d node_modules ==false ]; then npm install && npx npm-install-peers; fi
-d was unexpected at this time.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
有什么方法可以使它在 Windows 系统上也能正常工作?
解决方案
Windows 命令行不等同于其他系统上的 bash。您将需要通过某种方式安装 bash(例如 git 为 windows 附带的 bash),并在该 shell 中调用 npm。
这个包也是相关的,但同样不能解决问题:https ://www.npmjs.com/package/cross-env
评论的跟进。Node.js已经兼容该代码需要运行的任何地方。所以我建议在 npm script 命令中只使用 node 来运行脚本。
{
"install-dependencies": " node -e 'process.exit(require(`fs`).existsSync(`node_modules`) ? 0 : 1)' && echo 'succ'"
}
推荐阅读
- compiler-construction - 如何找到yacc规则的字符串?
- python - Python - 如果条件为 FALSE,则移至下一个列表元素
- python-3.x - 从整数列表中找到偶数的和
- javascript - 无法使用 jQuery 编辑、更新和取消表格单元格文本
- vagrant - 具有多集群的 Vagrantfile 设置
- python - 如何使用 python 将 LineString 中的多个点从 JSON 添加到 GeoJSON?
- embedded - 使用定时器 A0 以 1 kHz 的频率和 0.25 和 0.75 的占空比使连接到引脚 1.0 和 4.7 的板载 LED 闪烁
- python - 使用 Python 从 Azure 表存储中的表中获取最旧的 n 行
- excel - 意外的日期结果 - Excel VBA - 日月年混淆
- ssl - SEC_ERROR_REVOKED_CERTIFICATE 错误仅在 PC Firefox 上