如何在Vue-cli项目中更改端口号,以便它在另一个端口而不是8080上运行。
如何在Vue-cli项目中更改端口号,以便它在另一个端口而不是8080上运行。
vue-cli
3.x版本,你可以通过像下面这样的命令将端口号传递给npm
:
npm run serve -- --port 3000
然后访问 http://localhost:3000/
--
:https://cli.vuejs.org/guide/cli-service.html#using-the-binary。我一直在输入 npm run serve --port 3000
,这在我看来是很合理的,但出现了错误... 点赞! - toni07"serve":"vue-cli-service serve --port 3000"
。 - MatsLindhpackage.json
中进行更改,以便不必每次都给出端口,而是在npm run serve
命令中自动持久化。 - gluttony虽然来晚了,但我认为将所有答案整合成一个会更有帮助。
按照优先级(从高到低),分为Vue CLI v2 (webpack模板) 和 Vue CLI v3。
package.json
: 在serve
脚本中添加端口选项:scripts.serve=vue-cli-service serve --port 4000
--port
用于npm run serve
命令,例如:npm run serve -- --port 3000
。注意--
,这将把端口选项传递给npm脚本,而不是npm本身。从v3.4.1开始,应为vue-cli-service serve --port 3000
。$PORT
,例如:PORT=3000 npm run serve
.env
文件,更具体的env会覆盖较不具体的,例如:PORT=3242
vue.config.js
, devServer.port
,例如:devServer: { port: 9999 }
参考资料:
$PORT
,例如 PORT=3000 npm run dev
/config/index.js
: dev.port
参考文献:
"serve": "vue-cli-service serve --port 4000"
。 运行得很好! - RoccoBhost
、port
和 https
的值可能会被命令行标志覆盖。” https://cli.vuejs.org/config/#devserver 我有什么遗漏的吗?还有其他人有这个问题吗? - Ryan截至回答撰写时间(2018年5月5日),vue-cli
的配置托管在<your_project_root>/vue.config.js
。要更改端口,请参见下文:
// vue.config.js
module.exports = {
// ...
devServer: {
open: process.platform === 'darwin',
host: '0.0.0.0',
port: 8080, // CHANGE YOUR PORT HERE!
https: false,
hotOnly: false,
},
// ...
}
完整的vue.config.js
参考文档可以在此处找到:https://cli.vuejs.org/config/#global-cli-config
请注意,正如文档中所述,“webpack-dev-server”的所有选项(https://webpack.js.org/configuration/dev-server/)都可以在devServer
部分中使用。
Vue-cli webpack 模板的端口位于您的应用程序根目录下的 myApp/config/index.js
。
您只需要修改 dev
块内的 port
值即可:
dev: {
proxyTable: {},
env: require('./dev.env'),
port: 4545,
assetsSubDirectory: 'static',
assetsPublicPath: '/',
cssSourceMap: false
}
现在你可以使用 localhost:4545
访问你的应用程序
如果你有 .env
文件,最好从那里设置它
myApp/config/index.js
不存在! - exhumapackage.json
相同级别的位置创建vue.config.js
文件,并放置以下配置:module.exports = {
devServer: {
port: 3000
}
}
使用脚本进行配置:
npm run serve --port 3000
功能很强大,但如果你有更多的配置选项,我喜欢在配置文件中设置。你可以在文档中找到更多信息。
最好的方法是在您的 package.json
文件中更新服务器脚本命令。只需像这样添加 --port 3000
:
"scripts": {
"serve": "vue-cli-service serve --port 3000",
"build": "vue-cli-service build",
"inspect": "vue-cli-service inspect",
"lint": "vue-cli-service lint"
},
第一个选项:
打开 package.json 文件,并在 "serve" 部分中添加 "--port port-no"。
就像下面这样,我已经完成了。
{
"name": "app-name",
"version": "0.1.0",
"private": true,
"scripts": {
"serve": "vue-cli-service serve --port 8090",
"build": "vue-cli-service build",
"lint": "vue-cli-service lint"
}
第二个选项:如果您想通过命令提示符进行操作
npm run serve --port 8090
如果你想改变本地主机端口,可以在package.json中更改脚本标签:
"scripts": {
"serve": "vue-cli-service serve --port 3000",
"build": "vue-cli-service build",
"lint": "vue-cli-service lint"
},
在 webpack.config.js
文件中:
module.exports = {
......
devServer: {
historyApiFallback: true,
port: 8081, // you can change the port there
noInfo: true,
overlay: true
},
......
}
您可以在 module.exports
-> devServer
-> port
中更改端口。
然后重新启动 npm run dev
。您就可以得到它了。
哦,天啊!这并不是那么复杂的事情,以下这些答案同样有效。不过,对于这个问题的其他回答也很好。
如果你真的想使用 vue-cli-service
,并且想在你的 package.json
文件中设置端口号,即你的“vue create <app-name>”命令基本上创建的文件,你可以使用以下配置:--port 3000
。所以你整个脚本的配置应该像这样:
...
"scripts": {
"serve": "vue-cli-service serve --port 3000",
"build": "vue-cli-service build",
"lint": "vue-cli-service lint"
},
...
我正在使用 @vue/cli 4.3.1 (vue --version)
在 macOS 设备上。
我还添加了 vue-cli-service 参考文档:https://cli.vuejs.org/guide/cli-service.html