在package.json中使用“主页”,而不会弄乱本地主机的路径 [英] Using `"homepage"` in package.json, without messing up paths for localhost
问题描述
这个问题实际上直接来自我对上一个问题的答案。
我在我的 package.json
中添加了主页
,因为它是我的一个React应用托管在Github Pages上。 npm run build
的输出表明,现在可以部署 / build
目录,并且假定该项目正在托管在 / project_name /
。
但是在localhost上,项目 not 托管在 / project_name /
,因此js和CSS的请求路径被弄乱了(寻找 / project_name / static /...
而不是 / static /...
),并且应用程序损坏了。
在package.json中有homepage字段,以便它们可以部署到Github Pages(例如),同时仍可以使用正在运行的应用程序进行本地开发? >
文档用于 create-react-app
解释了如何从不同的相对路径提供相同的构建。
如果将首页设置为
首页:。,
资产将被保留相对于 index.html
的版本。然后,您可以将您的应用程序从 http://mywebsite.com
移到 http://mywebsite.com/relativepath
甚至 http://mywebsite.com/relative/path
无需重建它。
出于开发目的,使用纱线启动
或 npm启动
进行投放就足够了。该应用程序将在localhost
中可用
This question actually follows directly from my answer on a previous question.
I added a "homepage"
to my package.json
because it is a React app that I hosted on Github Pages. The output of npm run build
say that the /build
directory can now be deployed, and it assumes the project is being hosted at /project_name/
.
But on localhost, the project is not being hosted at /project_name/
, so the paths being requested for js and css are messed up (looking for /project_name/static/...
instead of /static/...
) and the app broken.
How can one have the homepage field in package.json so that they can deploy to Github Pages (for example) while still develop locally with a working app?
Docs for create-react-app
explains how to serve same build from different relative paths.
If you put homepage as
"homepage": ".",
assets will be served relative to index.html
. You will then be able to move your app from http://mywebsite.com
to http://mywebsite.com/relativepath
or even http://mywebsite.com/relative/path
without having to rebuild it.
For development purposes, serving using yarn start
or npm start
is good enough. App will be available in localhost
这篇关于在package.json中使用“主页”,而不会弄乱本地主机的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!