从React Redux中的package.json获取版本号(create-react-app) [英] Get version number from package.json in React Redux (create-react-app)
问题描述
OP如果其他人遇到此问题:该应用是使用 create-创建的react-app ,将导入限制在src文件夹内.但是,如果您将react-scripts升级到v1.0.11 ,它确实可以让您访问package.json.
OP 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.
我正在尝试从我的应用程序中的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.
我正在使用Jenkins,但尚未设置它来推动提交,但是我唯一的想法是从GitLab中的标签获取版本,但是我不知道该怎么做.,这会给存储库增加不必要的依赖性,所以我真的很想找到一个替代方法.
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.
我的文件结构如下:
--> RootAppFolder
|--> build
|--> node_modules
|--> public
|--> src
|--> Components
|--> Root.js
|
|--> package.json
因此要从Root.js访问package.json,我必须从'./../../package.json'导入packageJson,然后出现以下错误:>
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
./src/components/Root.js
未找到模块:您尝试导入./../../package.json不在项目src/中目录.不支持src/以外的相对导入.你可以要么将其移至src/内,要么从项目的目录中添加符号链接node_modules/.
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
列出src目录的内容,您将看到符号链接.
List contents of src directory and you'll see the symlink.
ls
#=> package.alias.json -> ../package.json
添加 .alias
有助于减少魔术"现象.对于其他人和您未来的自我.另外,它将帮助文本编辑器使它们分开.待会儿我会谢谢你的.只需确保您更新了JS代码即可从 ./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
.
另外,请看一下这个问题:在src目录之外的create-react-app导入限制
Also, please take a look at this question: The create-react-app imports restriction outside of src directory
这篇关于从React Redux中的package.json获取版本号(create-react-app)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!