Vue npm runserve 在随机端口上启动

2023-12-24

我正在尝试在端口 8080 上运行 Vue,但无法使其工作。我刚刚创建了一个全新的项目vue create .并运行它npm run serve,这会在随机端口上启动应用程序。

第一次尝试

运行 npm runserve 无需任何额外配置

$ npm run serve

> [email protected] /cdn-cgi/l/email-protection serve /Users/ne/projects/vue-demo
> vue-cli-service serve

 INFO  Starting development server...
Starting type checking service...
Using 1 worker with 2048MB memory limit
 98% after emitting CopyPlugin

 DONE  Compiled successfully in 4294ms                                                                                                              3:21:35 PM

No type errors found
Version: typescript 3.5.3
Time: 4267ms

  App running at:
  - Local:   http://localhost:20415/
  - Network: http://192.168.178.192:20415/

  Note that the development build is not optimized.
  To create a production build, run npm run build.

因此,首先我检查该端口上是否有另一个应用程序正在运行lsof -i :8080但这没有结果。

第二次尝试

所以第二次尝试是通过 cli 强制端口npm run serve -- --port 8080,它仍然在随机端口上启动应用程序,但浏览器控制台中没有错误。

第三次尝试

接下来我尝试在中配置应用程序vue.config.js.

module.exports = {
  devServer: {
    open: process.platform === 'darwin',
    host: 'localhost',
    port: 8080, // CHANGE YOUR PORT HERE!
    https: false,
    hotOnly: false,
  },
};

这也不起作用,甚至在浏览器控制台中抛出异常:

15:25:20.889 :8080/sockjs-node/info?t=1566048320873:1 Failed to load resource: net::ERR_CONNECTION_REFUSED
15:25:20.910 client?81da:172 [WDS] Disconnected!
close @ client?81da:172
15:25:21.982 :8080/sockjs-node/info?t=1566048321978:1 Failed to load resource: net::ERR_CONNECTION_REFUSED
15:25:23.079 :8080/sockjs-node/info?t=1566048323075:1 Failed to load resource: net::ERR_CONNECTION_REFUSED
15:25:25.097 :8080/sockjs-node/info?t=1566048325091:1 Failed to load resource: net::ERR_CONNECTION_REFUSED
15:25:29.151 VM14:1 GET http://localhost:8080/sockjs-node/info?t=1566048329145 net::ERR_CONNECTION_REFUSED

包.json

我添加了 package.json,因为我可能在这里遗漏了一些东西。

{
  "name": "frontend",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "serve": "vue-cli-service serve",
    "build": "vue-cli-service build",
    "lint": "vue-cli-service lint",
    "test:unit": "vue-cli-service test:unit"
  },
  "dependencies": {
    "core-js": "^2.6.5",
    "vue": "^2.6.10",
    "vue-class-component": "^7.0.2",
    "vue-property-decorator": "^8.1.0",
    "vue-router": "^3.0.3",
    "vuex": "^3.0.1"
  },
  "devDependencies": {
    "@types/jest": "^23.1.4",
    "@vue/cli-plugin-babel": "^3.10.0",
    "@vue/cli-plugin-eslint": "^3.10.0",
    "@vue/cli-plugin-typescript": "^3.10.0",
    "@vue/cli-plugin-unit-jest": "^3.10.0",
    "@vue/cli-service": "^3.10.0",
    "@vue/eslint-config-airbnb": "^4.0.0",
    "@vue/eslint-config-typescript": "^4.0.0",
    "@vue/test-utils": "1.0.0-beta.29",
    "babel-core": "7.0.0-bridge.0",
    "babel-eslint": "^10.0.1",
    "eslint": "^5.16.0",
    "eslint-plugin-vue": "^5.0.0",
    "node-sass": "^4.9.0",
    "sass-loader": "^7.1.0",
    "ts-jest": "^23.0.0",
    "typescript": "^3.4.3",
    "vue-template-compiler": "^2.6.10"
  }
}

我缺少什么?


注意:此问题特定于node-portfinderv1.0.22 https://github.com/http-party/node-portfinder/releases/tag/v1.0.22。已解决v1.0.23 https://github.com/http-party/node-portfinder/releases/tag/v1.0.23这是 v1.0.21 的重新标记。

这似乎是 portfinder 中的一个功能,它使用随机序列来分配可用端口。

Trying:

const portfinder = require('portfinder');
portfinder.basePort=8080
portfinder.getPortPromise().then((port) => { console.log(port) })

返回类似:

9567

即使端口 8080 可用。

最近的行为发生了变化commit https://github.com/http-party/node-portfinder/commit/eeeaba108cbb63b7934e7e0837b024698e0344ce#diff-8a54d09b8f24635c2b2cc156ac31ed21。在端口尝试从基本端口增加到最高端口之前。它随版本一起提供v1.0.22 https://github.com/http-party/node-portfinder/releases/tag/v1.0.22

选项1:打补丁

为了使用8080端口,我修补了该文件node_modules/@vue/cli-service/lib/commands/serve.js添加第 322 行portfinder.highestPort = portfinder.basePort + 1

portfinder.basePort = args.port || process.env.PORT || projectDevServerOptions.port || defaults.port
portfinder.highestPort  = portfinder.basePort + 1
const port = await portfinder.getPortPromise()

选项 2:在行为更改之前安装 portfinder

等待 portfinder/vue-cli 选择解决方案的另一种解决方法是安装旧的 portfinder:

npm install [email protected] /cdn-cgi/l/email-protection
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Vue npm runserve 在随机端口上启动 的相关文章

随机推荐

  • 透明、点击、始终位于顶部的 JFrame [关闭]

    很难说出这里问的是什么 这个问题是含糊的 模糊的 不完整的 过于宽泛的或修辞性的 无法以目前的形式得到合理的回答 如需帮助澄清此问题以便重新打开 访问帮助中心 help reopen questions 因此 我目前有一个透明的 JFram
  • TweenLite 没有动画

    我正在尝试使用 GreenSocks TweenLite for Javascript 制作一个简单的上边距动画 我已经使用过该库很多次了 但是由于某种原因 这次它不起作用 注意 onComplete 正在触发 但是无论我使用什么元素 我都
  • 内容安全策略:允许所有外部图像?

    我希望只允许来自本地服务器的脚本 但有某些例外 例如 jQuery 等 但可以灵活地加载外部图像 我知道有一个像这样的指令 Content Security Policy script src self https apis google
  • 将 ADT 和 Android sdk 工具更新到最新版本 22 后出现类未找到错误

    早些时候它工作正常 当我将我的 adt 和 android sdk 工具更新到最新版本 rev 22 时 我的应用程序停止工作 下面是 logcat 输出 请帮忙 05 16 16 36 01 922 E AndroidRuntime 12
  • 使用 Highcharts.js 的圆边仪表

    我正在使用 Highcharts 创建自定义仪表 窗格形状应如所附图片所示为圆形 想知道是否有人知道如何使用该库实现此布局 这是http jsfiddle net ao9fv2yh http jsfiddle net ao9fv2yh 我正
  • Git:忽略已编译的 Google Go

    我编译的 Go 代码在 Linux 上没有以扩展结尾 对于处理忽略 gitignore 文件中的这些内容有什么技巧吗 如果您正在使用go您可以使用构建代码的工具 o标志来指定输出文件名 因此您可以使用go build o bin elf然后
  • .NET 世界中的 ORM 和 SOA [关闭]

    Closed 这个问题是基于意见的 help closed questions 目前不接受答案 根据我的经验 NET 的主要 ORM 框架 NHibernate http nhforge org Linq 转 Sql http msdn m
  • 为什么 Android 会截断我的 ActionBar 标题?

    在我的应用程序中 我更改了 ActionBar 中显示的每个片段的标题 当我第一次启动我的应用程序时 我收到了一个请求列表 因此我的标题是 我的请求 20 然后 当您单击该列表中的某个项目时 它会替换我的活动中的一个片段 并将标题设置为 操
  • 如何在子例程内引用哈希?

    我正在尝试使用模块的 tie 功能配置 Ini文件 http search cpan org shlomif Config IniFiles 2 65 lib Config IniFiles pm但我不知道如何引用子例程内部的哈希 如果我从
  • 查询或过滤最小字段值?

    示例 存储在索引中的文档表示测试分数和有关每个测试的元数据 test 1 user 1 score 100 meta other data test 2 user 2 score 65 meta other data test 3 user
  • Pylint 未在 VScode 中按预期运行

    当我通过 shell 运行时pylint pylint decorator py No config file found using default configuration Module decorator C 7 0 Unneces
  • EC2 无法解析私有 DNS 主机名

    我是 Amazon Web Service AWS 的新手 我刚刚创建了一个 VPC 和一个位于其中的子网 但是 我无法解析该子网中的任何主机名 当我停留在该子网中的任何主机中时 ec2 user ip 192 168 1 86 nsloo
  • 根据 rowwise 函数 (dplyr) 过滤行

    您能帮我在下面的最后一个命令中使用过滤dplyr代替apply 我试图解决发布的问题here https twitter com delta dc status 585478403463245826 photo 1 library gtoo
  • 使用Spring Beans和不使用Spring Beans有什么区别?

    可能我会得到很多反对票 但是对于我来说是否使用 bean 的所有事实都让我感到困惑 让我们假设这个例子 interface ICurrency String getSymbol public class CurrencyProcessor
  • boost::filesystem::path(std::wstring) 抛出异常

    这段代码 boost filesystem is directory usr include 工作正常 这段代码 boost filesystem is directory L usr include 抛出异常 抛出一个后终止调用 std
  • 首次加载时 WPF 验证不会触发

    在 Prism 应用程序中 我想使用验证 我已经在 ViewModel 中实现了 INotifyDataError 接口 但我发现首次加载控件时不会触发验证解决方案 然后我发现了同样的问题 比如 wpf 验证绑定在首次加载时未触发 我找到了
  • 使用 ACR122 在卡模拟模式下将数据写入 Nexus 4

    我正在尝试通过 NFC 即卡模拟模式 向 Nexus 4 发送一些数据 我尝试了许多命令 APDU 例如写入和更新 APDU 但无法让它们工作 我想说的是 我想在选择 APDU 命令后向手机发送一些数据 不是 AID 提前致谢 Bader
  • Doctrine 迁移:“”命名空间中没有定义命令

    我正在尝试将 Doctrine Migrations 设置为独立程序 但遇到了一些麻烦 我在同一个文件夹中有doctrine migrations phar 和migrations yml migrations yml 包含以下内容 nam
  • 邮件:无法打开流:权限被拒绝?

    我在使用 php 发送邮件时收到此警告 警告 邮件 1 function mail 无法打开流 home 中的权限被拒绝 使用 ssmtp 和 gmail 作为 smtp PHP 5 3 1 日志中没有任何内容 没有错误 邮件到达目的地 文
  • Vue npm runserve 在随机端口上启动

    我正在尝试在端口 8080 上运行 Vue 但无法使其工作 我刚刚创建了一个全新的项目vue create 并运行它npm run serve 这会在随机端口上启动应用程序 第一次尝试 运行 npm runserve 无需任何额外配置 np