Vue npm run serve starts on random port

前端 未结 3 1240
长发绾君心
长发绾君心 2021-02-01 06:15

I\'m trying to run Vue on port 8080, but can\'t get that to work. I just created a brand new project with vue create . and ran it with npm run serve, w

相关标签:
3条回答
  • 2021-02-01 06:55

    Note: This issue is specific to node-portfinder v1.0.22. It was resolved in v1.0.23 that is a retag of v1.0.21.

    This seems a feature in portfinder that used random series to allocate an available port.

    Trying:

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

    return something like:

    9567

    Even if port 8080 is available.

    The behaviour change recently in this commit. Before the port were try increasing from basePort to highestport. It comes with the release v1.0.22

    Option1: Patching

    In order to use the port 8080, I patched the file node_modules/@vue/cli-service/lib/commands/serve.js adding line 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()
    

    Option2: Install portfinder previous to the behaviour change

    Another way to workaround waiting for portfinder/vue-cli to choose a solution is to install old portfinder with :

    npm install portfinder@1.0.21
    
    0 讨论(0)
  • 2021-02-01 06:56

    Most of time the error occurred when something is running on the same port. But as you mentioned above you have checked it out and there is nothing on it.

    Have you tried to restart or shutdown your system and run the server again if not give it a try some of the time its worked for me.

    Secondly it would be help full if you share the object of Scripts in package.json.

    I have gone through your issue and found this article may be this will help you out. Let me know if the error still exists.

    0 讨论(0)
  • 2021-02-01 07:13

    You can temporarily rollback portfinder by placing

    "resolutions": {
      "@vue/cli-service/portfinder": "1.0.21"
    }
    

    in your package.json file and run yarn install afterwards.

    0 讨论(0)
提交回复
热议问题