aws-amplify / amplify-hosting

AWS Amplify Hosting provides a Git-based workflow for deploying and hosting fullstack serverless web applications.
https://aws.amazon.com/amplify/hosting/
Apache License 2.0
461 stars 116 forks source link

$npm_package_version no longer works? #1472

Open CodySwannGT opened 3 years ago

CodySwannGT commented 3 years ago

Hi there.

We have a number of Amplify apps and all of them set an environment variable called "REACT_APP_VERSION" to "$npm_package_version"

In all our previous apps, it works as intended in our code:

Version {process.env.REACT_APP_VERSION}

produces "Version 1.0.1" (or whatever)

However, on a newly deployed app, we're getting

"Version $npm_package_version"

hsspain commented 3 years ago

@CodySwannGT I am sorry you are having issues. Has anything changed in your build specification between the times it worked and when it didn't? Can you share that build spec?

CodySwannGT commented 3 years ago

Exact same build settings

version: 0.1
frontend:
  phases:
    preBuild:
      commands:
        - cd react-client
        - npm install
    build:
      commands:
        - npm run build
  artifacts:
    baseDirectory: /react-client/build
    discardPaths: true
    files:
      - '**/*'
  cache:
    paths:
      - node_modules/**/*
      - react-client/node_modules/**/*
CodySwannGT commented 3 years ago

Any word on this?

nbyodai commented 3 years ago

I am also seeing this for my application. Please help address.

r-moore commented 3 years ago

Any news? This seems to be a regression?

CodySwannGT commented 3 years ago

Just started happening on another application. Any help is greatly appreciated

CodyMoore240 commented 3 years ago

Any word on this? Just experienced this issue.

r-moore commented 3 years ago

I am going to open a support case via AWS console as there just seems to be tumbleweed here....

kahdojay commented 3 years ago

@CodySwannGT @r-moore @CodyMoore240 @nbyodai This looks to be related to the version of npm we're using:

https://github.com/npm/cli/issues/2731

We'll need to update the version we use in the default build container. As a workaround in the meantime, can you try updating npm as part of your build commands, for example:

version: 0.1
frontend:
  phases:
    preBuild:
      commands:
        - npm install npm@latest -g
...

Let us know if this doesn't work

r-moore commented 3 years ago

I did give this a try and can see npm install npm@latest -g running succesfully in the prebuild logs but it doesn't seem to have worked for me - app still shows $npm_package_version

I wonder if my environment variables are being resolved before the npm upgrade above has executed?:

Screenshot 2021-10-11 at 15 44 27

Will try modifying my build script in package.json to something like this: "build": "REACT_APP_VERSION=$npm_package_version yarn build"

CodySwannGT commented 3 years ago

Tried that and did not work

marceloch2 commented 2 years ago

Same here for me, any news about this issue?

james-e-morris commented 2 years ago

Is it not possible to access NPM package version when hosting with Amplify?

lucasltv commented 2 years ago

Just add this script to run with cross-env

"build": "cross-env REACT_APP_VERSION=$npm_package_version && craco build",
thiago-soliveira commented 1 year ago

Adding the following line to the amplify.yml file did the trick for me.

build:
  commands:
    - env | grep -e NEXT_PUBLIC_APP_VERSION >> .env # this line
    - npm run build

I have also added the following env variable to aws amplify NEXT_PUBLIC_APP_VERSION=$npm_package_version

james-e-morris commented 1 year ago

Adding the following line to the amplify.yml file did the trick for me.

build:
  commands:
    - env | grep -e NEXT_PUBLIC_APP_VERSION >> .env # this line
    - npm run build

I have also added the following env variable to aws amplify NEXT_PUBLIC_APP_VERSION=$npm_package_version

This worked perfectly, thank you! I thought I tried this, but I think maybe I updated my yml file previously via the web console, but was still overwriting it with my amplify.yml file in my repo and didn't realize it.

karpolan commented 2 months ago
- env | grep -e NEXT_PUBLIC_APP_VERSION >> .env

This approach is not working :(

NEXT_PUBLIC_APP_VERSION contains "$npm_package_version" as string...