从 React Redux (create-react-app) 中的 package.json 获取版本号 [英] Get version number from package.json in React Redux (create-react-app)

查看:20
本文介绍了从 React Redux (create-react-app) 中的 package.json 获取版本号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

OP 编辑​​:如果其他人遇到此问题:该应用程序是使用 create- 创建的react-app,将导入限制在 src 文件夹内.但是,如果您将 react-scripts 升级到 v1.0.11,它确实可以让您访问 package.json.

我正在尝试从我的应用程序中的 package.json 获取版本号.

我已经试过了 这些建议,但它们都没有奏效,因为我无法从 src 文件夹外部访问 package.json (可能是由于 React,我是新手).将 package.json 移动到 src 意味着我无法从我的根文件夹运行 npm installnpm version minornpm run build.我试过使用 process.env.npm_package_version 但这导致未定义.

我正在使用 Jenkins,我还没有设置它来推送提交,但我唯一的想法是从 GitLab 中的标签中获取版本,但我不知道该怎么做,并且它会给 repo 添加不必要的依赖,所以我真的很想找到一个替代方案.

我的文件结构是这样的:

-->根应用程序文件夹|-->建造|-->节点模块|-->上市|-->源文件|-->组件|-->根.js||-->包.json

所以要从 Root.js 访问 package.json 我必须做 import packageJson from './../../package.json' 然后我得到以下错误:<块引用>

./src/components/Root.js

未找到模块:您试图导入./../../package.json 不属于项目 src/目录.不支持 src/之外的相对导入.你可以要么将其移动到 src/中,要么从项目的node_modules/.

解决方案

根据您的编辑,我建议尝试:

import packageJson from '/package.json';

您也可以尝试创建符号链接:

# 从项目根目录.cd src;ln -s ../package.json package.alias.json

列出 src 目录的内容,您将看到符号链接.

ls#=>package.alias.json ->../package.json

添加 .alias 有助于减少魔法"当你看到这个时,为了他人和你未来的自己.另外,它会帮助文本编辑器将它们分开.以后你会感谢我的.只需确保更新您的 JS 代码以从 ./package.alias.json 而不是 ./package.json 导入.

另外,请看一下这个问题:src 目录外的 create-react-app 导入限制

OP EDIT: If anyone else comes across this: the app was created using create-react-app, which limits importing to within the src folder. However if you upgrade react-scripts to v1.0.11 it does let you access package.json.

I'm trying to get the version number from package.json in my app.

I've already tried these suggestions, but none of them have worked as I can't access package.json from outside the src folder (might be due to React, I'm new to this). Moving package.json into src then means I can't run npm install, npm version minor, and npm run build from my root folder. I've tried using process.env.npm_package_version but that results in undefined.

I'm using Jenkins, and I haven't set it up to push the commits up yet, but the only idea I have is to get the version from the tags in GitLab, but I have no idea how to do that, and it would add unnecessary dependency to the repo, so I would really like to find an alternative.

EDIT: My file structure is like:

--> RootAppFolder
    |--> build
    |--> node_modules
    |--> public
    |--> src
         |--> Components
              |--> Root.js
    |
    |--> package.json

So to access package.json from Root.js I have to do import packageJson from './../../package.json' and then I get the following error:

./src/components/Root.js

Module not found: You attempted to import ./../../package.json which falls outside of the project src/ directory. Relative imports outside of src/ are not supported. You can either move it inside src/, or add a symlink to it from project's node_modules/.

解决方案

From your edit I would suggest to try:

import packageJson from '/package.json';

You could also try to create a symlink:

# From the project root.
cd src; ln -s ../package.json package.alias.json

List contents of src directory and you'll see the symlink.

ls
#=> package.alias.json -> ../package.json

Adding the .alias helps reduce the "magic" for others and your future self when looking at this. Plus, it'll help text editors keep them apart. You'll thank me later. Just make sure you update your JS code to import from ./package.alias.json instead of ./package.json.

Also, please take a look at this question: The create-react-app imports restriction outside of src directory

这篇关于从 React Redux (create-react-app) 中的 package.json 获取版本号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆