【问题标题】:Npm "scripts": "start" run express and open urlnpm "scripts": "start" 运行 express 并打开 url
【发布时间】:2016-05-17 15:17:16
【问题描述】:

我在package.json 中有这个启动参数

"scripts": {
    "start": "node bin/www"
  },

当我输入npm start 时,它正在运行我的快递应用程序。

但我希望浏览器同时打开http://localhost:8081。我怎样才能对start 说也打开我的本地网址?

点赞:"start": "node bin/www, http://localhost:8081"

所以当我输入npm satrt 时,它会运行我的快速应用程序并同时打开网址。

【问题讨论】:

    标签: node.js angular express npm


    【解决方案1】:

    据我所知,这就像编写 bash 命令:

    // Windows
    "start":"start http://localhost:8081 & node bin/www"
    
    // Mac
    "start":"open http://localhost:8081 && node bin/www"
    
    // Linux
    "start":"xdg-open http://localhost:8081 && node bin/www"
    

    【讨论】:

    • 我不是 Windows,"start":"node bin/www & start http://localhost:8081" 不会工作
    • 但是,如果我在应用程序中有错误,那么它将使用指定的 url 启动新的浏览器窗口,但如果没有错误,则不会显示窗口
    • 好的,我想我知道为什么,请参阅更新后的答案。 Windows 中的& 将在第一个命令之后运行第二个命令。当节点失败时,它会完成该命令,然后打开浏览器。当节点没有失败时,它不会完成并且浏览器窗口永远不会打开。
    【解决方案2】:

    对于跨平台支持,请使用open-cli

    安装它:

    npm install --save-dev open-cli
    

    将其添加到您的脚本中:

    "start": "open-cli http://localhost:8081 && node bin/www"
    

    【讨论】:

    • 很好的解决方案。顺便说一句:要在 url 中转义 &,请使用 \\&,即 open-cli https://localhost:8081?var1=foo\\&var2=bar && node bin/www
    【解决方案3】:

    您只需按正确的顺序使用start

    "start": "npm run dev & start http://localhost:8000",
    

    不好

    "start": "start http://localhost:8000 & npm run dev",
    

    不错

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-03-25
      • 2022-12-14
      • 2020-06-05
      • 1970-01-01
      • 2017-11-07
      • 1970-01-01
      • 2014-11-14
      相关资源
      最近更新 更多