2

I'll often run into the following error when starting the server on a Rails project:

========================================
  Your Yarn packages are out of date!
  Please run `yarn install --check-files` to update.
========================================

Given that the system knows what to do, is there a way to configure Yarn and Rails to automatically perform this action when required (ie: when running rails server and there are missing Yarn packages)?

brass monkey
  • 5,841
  • 10
  • 36
  • 61
Ivar
  • 5,102
  • 2
  • 31
  • 43

1 Answers1

2

In your package.json file, just add an extra command to the script say, "start" that starts your server chaining the yarn install --check-files command (reinstall all packages that have changed) and the start command using && like this:

"scripts": {
  "start": "yarn install --check-files && someStartCommand someFile.xyz",
  "someOtherScript": "someOtherCommand someOtherFile.xyz",
}

Alternatively, you can use the yarn upgrade command if you want to update your packages to their latest version based on the version range specified in the package.json file by adding an extra command to the script say, "start" that starts your server chaining the yarn upgrade command and the start command using && like this:

"scripts": {
  "start": "yarn upgrade && someStartCommand someFile.xyz",
  "someOtherScript": "someOtherCommand someOtherFile.xyz",
}

You can also add the --latest flag to your yarn upgrade command if you want to ignore the version range specified in package.json and instead install the version specified by the latest tag (potentially upgrading the packages across major versions).

AndrewL64
  • 15,794
  • 8
  • 47
  • 79
  • This is promising, but it doesn't clarify the specifics when invoked with the Rails server start command. I've updated the question to clarify. – Ivar Jan 08 '20 at 19:05
  • What exactly do you mean by "specifics" here? – AndrewL64 Jan 08 '20 at 19:06
  • The missing pacakges notification appears when I invoke the Rails server (ie: `rails server`) - I've started digging into how Rails invokes Yarn so I can make use of your helpful suggestion to use the Yarn scripts, but it doesn't appear that the default Rails config uses the yarn `start` script. (Still investigating) The Yarn integration with Rails is via https://github.com/rails/webpacker#yarn-integrity – Ivar Jan 08 '20 at 19:09
  • 1
    The "start" script is just a dummy name that I used. You need to check your `pacakge.json` file to find out which script invokes the rails server and chain the yarn command there. – AndrewL64 Jan 08 '20 at 19:10
  • Instead of installing missing files based on scripts, Is it possible to run the install command as precursor to the integrity check? The webpacker gem runs https://github.com/rails/webpacker#yarn-integrity – Ivar Jan 08 '20 at 19:15
  • Im not too familiar with webpacker tbh but let me go through the docs and see if I can figure it out. – AndrewL64 Jan 08 '20 at 19:30
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/205625/discussion-between-ivar-and-andrewl64). – Ivar Jan 08 '20 at 19:38