node.js 从 React Redux (create-react-app) 中的 package.json 获取版本号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45978230/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get version number from package.json in React Redux (create-react-app)
提问by Baldeep
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.11it does let you access package.json.
OP 编辑:如果其他人遇到此问题:该应用程序是使用create-react-app 创建的,这将导入限制在 src 文件夹内。但是,如果您将 react-scripts 升级到 v1.0.11,它确实允许您访问 package.json。
I'm trying to get the version number from package.json in my app.
我正在尝试从我的应用程序中的 package.json 获取版本号。
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 buildfrom my root folder. I've tried using process.env.npm_package_versionbut that results in undefined.
我已经尝试过这些建议,但它们都没有奏效,因为我无法从 src 文件夹外部访问 package.json(可能是由于 React,我是新手)。移动的package.json到SRC那么意味着我不能跑npm install,npm version minor和npm run build从我的根文件夹。我试过使用,process.env.npm_package_version但结果未定义。
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.
我正在使用 Jenkins,我还没有设置它来推送提交,但我唯一的想法是从 GitLab 中的标签中获取版本,但我不知道如何做到这一点,它会给 repo 添加不必要的依赖,所以我真的很想找到一个替代方案。
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:
所以要从 Root.js 访问 package.json 我必须这样做import packageJson from './../../package.json',然后我收到以下错误:
./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/.
./src/components/Root.js
未找到模块:您试图导入 ./../../package.json ,该文件位于项目 src/ 目录之外。不支持 src/ 之外的相对导入。您可以将其移动到 src/ 中,也可以从项目的 node_modules/ 中向其添加符号链接。
采纳答案by kenfire
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.
列出 src 目录的内容,您将看到符号链接。
ls
#=> package.alias.json -> ../package.json
Adding the .aliashelps 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.jsoninstead of ./package.json.
添加这些.alias有助于减少对他人和未来的自己的魔力。另外,它会帮助文本编辑器将它们分开。以后你会感谢我的。只需确保更新您的 JS 代码以导入 from./package.alias.json而不是./package.json.
Also take a look at this question: The create-react-app imports restriction outside of src directory
也看看这个问题: src 目录外的 create-react-app 导入限制
回答by talves
Solving this without importing and exposing package.jsonto the create-react-app
解决这个问题而不导入和暴露package.json给create-react-app
Requires: version 1.1.0+of create-react-app
需要:create-react-app 1.1.0+ 版本
.env
.env
REACT_APP_VERSION=$npm_package_version
REACT_APP_NAME=$npm_package_name
index.js
index.js
console.log(`${process.env.REACT_APP_NAME} ${process.env.REACT_APP_VERSION}`)
Note:the version (and many other npm config params) can be accessed
注意:可以访问版本(以及许多其他npm 配置参数)
回答by Vorathep Sumetphong
Try this.
尝试这个。
// in package.json
"version": "1.0.0"
// in index.js
import packageJson from '../package.json';
console.log(packageJson.version); // "1.0.0"
回答by Hyman sun
I don't think getting version by 'import' or 'require' package is correct. You can add a script in you package.json
我认为通过 'import' 或 'require' 包获取版本是不正确的。您可以在 package.json 中添加脚本
"start": "REACT_APP_VERSION=$npm_package_version react-app-script start",
You can get it by "process.env.REACT_APP_VERSION" in any js files.
您可以通过任何 js 文件中的“process.env.REACT_APP_VERSION”获取它。
It also works in build scripts, like this:
它也适用于构建脚本,如下所示:
"build": "REACT_APP_VERSION=$npm_package_version react-app-script build",

