Get version number from package.json in React Redux (create-react-app)

后端 未结 4 1221
悲哀的现实
悲哀的现实 2021-02-01 11:34

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-scri

4条回答
  •  别那么骄傲
    2021-02-01 12:25

    Solving this without importing and exposing package.json to the create-react-app

    Requires: version 1.1.0+ of create-react-app

    .env

    REACT_APP_VERSION=$npm_package_version
    REACT_APP_NAME=$npm_package_name
    

    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

    Note 2: changes to the .env file will be picked only after you restart the development server

提交回复
热议问题