Cache and use file later in gitlab CI/CD pipeline

I have a project which has many subfolder. I have file named "build.js". When I push the project to repository, in pipeline first this build.js run. In these build.js all subfolders build then json file created about these folders.

When I push my code after little bit changes, in pipeline build.js search for json file. But it can't find.I want to use this json file second or third push requests for example because it created first push request.

Should I use cache or artifacts ? How should I make this operation in .yml file ?

Currently build stage is like that in my .gitlab-ci.yml file:

build:
  stage: build
  image: $BUILD_IMAGE
  only:
    - develop
    - master
    - merge_requests
  script:
    - node ./src/scripts/build.js
  cache:
    key:
      files:
        - package.json     
    paths:
      - node_modules
    policy: pull
  artifacts:
    paths:
      - hash.json
    expire_in: 1 week
  retry:
    max: 2
    when:
      - runner_system_failure
      - unknown_failure