You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过package.json脚本调用默认程序打开文件?配置npm start打开图片

通过package.json脚本调用程序打开文件的方案

一、调用系统默认程序打开文件

分平台直接命令

  • Windows:使用start命令,脚本写法:"start": "start image.png"
  • macOS:使用open命令,脚本写法:"start": "open image.png"
  • Linux:使用xdg-open命令,脚本写法:"start": "xdg-open image.png"

跨平台兼容方案

借助npm包open实现跨平台支持,无需全局安装:

  1. 安装依赖:
npm install open --save-dev
  1. 修改package.json的scripts:
{
    "name": "open-image",
    "version": "1.0.0",
    "scripts": {
        "start": "open image.png"
    }
}

二、Windows系统指定用Windows照片查看器打开image.png

Windows照片查看器需要通过rundll32.exe调用其DLL文件,修改scripts如下:

{
    "name": "open-image",
    "version": "1.0.0",
    "scripts": {
        "start": "rundll32.exe \"C:\\Program Files\\Windows Photo Viewer\\PhotoViewer.dll\", ImageView_Fullscreen image.png"
    }
}

也可以用正斜杠避免转义:

{
    "name": "open-image",
    "version": "1.0.0",
    "scripts": {
        "start": "rundll32.exe \"C:/Program Files/Windows Photo Viewer/PhotoViewer.dll\", ImageView_Fullscreen image.png"
    }
}

注:你原配置里直接写"start": "image.png"在Windows下可能不生效,因为npm脚本的执行环境未自动关联默认打开程序,用start命令或上述指定程序的写法更可靠。

内容的提问来源于stack exchange,提问作者stackers

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 05:27:26