覆盖或更改package.json“主页”值(Overwritting or changing package.json “homepage” value)

编程入门 行业动态 更新时间:2024-10-28 02:33:19
覆盖或更改package.json“主页”值(Overwritting or changing package.json “homepage” value)

我正在尝试覆盖或更改我的package.json中的另一个文件中的值。 基本上,package.json有一个“主页”值,当我运行npm run build时会生成该值。 我希望能够从我的config.jsconfig.production.json文件中更改该值。 我希望我的环境值都在配置文件中,因此更容易修改。 但是,当我运行npm run build ,它仍会从package.json加载我的主页值。

的package.json

{ "homepage": "/company/portal" }

config.production.json

{ "configPath": "/newCompany/portal" }

感谢您的帮助

I'm currently trying to overwrite or change a value in my package.json from another file. Basically, package.json has a "homepage" value that gets built when I run npm run build. I wish to be able to change that value from my config.js or config.production.json files. I want my environment values all in the config files so it is easier to modify. However, when I run npm run build, it still loads my homepage value from the package.json.

package.json

{ "homepage": "/company/portal" }

config.production.json

{ "configPath": "/newCompany/portal" }

Thank you for your help

最满意答案

如果您通常意味着如何通过浏览器中的脚本在客户端计算机中编写文件,那么这不是一件容易被允许的事情,并且您还会遇到很多跨浏览器的问题,实现它为0。

您可以尝试看看这个看起来相当不错的库:

https://eligrey.com/demos/FileSaver.js/

您还可以考虑向node.js应用程序启动AJAX请求,例如,它可以在您的系统上轻松地将文件写入您的文件或提供下载,或者通常是服务器端,它将提供要下载到客户端的文件。

但是,从您解释的情况来看,您只需要更改系统中的配置文件,因此考虑到您使用的是node.js,最简单的方法是使用node.js

通常强烈建议不要将您的env变量值放在JSON文件中。 看起来你可以保持秩序,但很可能你的代码会被推到某个地方,所以打开JSON文件,任何人都可以看到你所有的env变量值。 这不是一件好事。

在我看来,你应该考虑编写你的构建脚本,以便在调用时获取参数,然后调用构建传递参数。 例如:

npm build --production npm build --development

根据参数,您将动态设置正确的配置。

我希望这有帮助

If you mean in general how to write a file in the client machine through a script in the browser, it isn't a thing allowed so easily and you will have also a lot of cross browsers issues implementing it by 0.

You could try to take a look to this library which seems quite good:

https://eligrey.com/demos/FileSaver.js/

You can also think to start an AJAX request to a node.js application for example which will write you the file easily on your system or provide a download, or in general to server side which will provide a file to be downloaded to the client.

But, from the case you explained, you just need to change the config file in your system, so considering you are using node.js, the easiest way is using node.js

It is usually strongly discouraged to put your env variables values inside a JSON file. It can seem that you keep things in order but most probably your code will be pushed somewhere and so opening the JSON file, anyone can see all your values of the env variables. And this is not a good thing.

In my opinion you should think to write your build script in order to take arguments when called, and then call the build passing the parameters. For example:

npm build --production npm build --development

Depending on the parameters, you will set up the right configuration on the fly.

I hope this helps

更多推荐

本文发布于:2023-07-22 06:35:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1217744.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:主页   json   package   homepage   changing

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!