Search code examples
node.jsyarnpkgpackage.jsondotfilesalfred

Best practices for managing yarn install for different environments


I use the same package.json for my dot files on MacOS and Linux. This was not a problem until recently when one of the packages stopped installing on Linux. (The reason for the failure is that the package is expecting Alfred, a MacOS app, to be installed). When it's not found in my Linux env, the installation bails.

What's the best way to manage this kind of a thing? Should I have two different package.json files? One for generic stuff and one for Mac-specific packages?

For reference, the dependencies I've currently got are:

  "devDependencies": {
    "acorn": "^7.1.0"
  },
  "dependencies": {
    "alfred-fkill": "^0.4.1",
    "bash-language-server": "^1.6.1",
    "eslint": "^6.6.0",
    "fkill-cli": "^5.2.0",
    "jsonlint": "^1.6.3",
    "prettier": "^1.19.1"
  }

The error I get under Linux is:

$ yarn upgrade
yarn upgrade v1.19.0
[1/4] Resolving packages...
warning alfred-fkill > fkill > taskkill > execa > cross-spawn-async@2.2.5: cross-spawn no longer requires a build toolchain, use it instead
warning bash-language-server > turndown > jsdom > left-pad@1.3.0: use String.prototype.padStart()
warning jsonlint > nomnom@1.8.1: Package no longer supported. Contact support@npmjs.com for more info.
[2/4] Fetching packages...
[3/4] Linking dependencies...
[4/4] Rebuilding all packages...
error /home/oalders/dot-files/node_modules/alfred-fkill: Command failed.
Exit code: 1
Command: alfy-init
Arguments:
Directory: /home/oalders/dot-files/node_modules/alfred-fkill
Output:
{ Error: Command failed: alfred-link
Error: Alfred preferences not found at location /home/oalders/Library/Application Support/Alfred/prefs.json
    at module.exports (/home/oalders/dot-files/node_modules/resolve-alfred-prefs/index.js:44:9)
    at makeError (/home/oalders/dot-files/node_modules/alfy/node_modules/execa/index.js:174:9)
    at Promise.all.then.arr (/home/oalders/dot-files/node_modules/alfy/node_modules/execa/index.js:278:16)
    at process._tickCallback (internal/process/next_tick.js:68:7)
  code: 1,
  stdout: '',
  stderr:
   'Error: Alfred preferences not found at location /home/oalders/Library/Application Support/Alfred/prefs.json\n    at module.exports (/home/oalders/dot-files/node_modules/resolve-alfred-prefs/index.js:4
4:9)\n',
  failed: true,
  signal: null,
  cmd: 'alfred-link',
  timedOut: false,
  killed: false }
info Visit https://yarnpkg.com/en/docs/cli/upgrade for documentation about this command.

Solution

  • My solution to this is that I've decided to abuse devDependencies. I've declared alfred-fkill (the MacOS-only node module) as a dev dependency. Then on Linux I run yarn install --production=true. This ignores the developer deps and I no longer have a failed installation. On my Mac I'll run yarn install and it will install everything, as it did before. This works, but it's weird.

    I'm left with:

      "devDependencies": {
        "alfred-fkill": "^0.4.1"
      },
      "dependencies": {
        "acorn": "^7.1.0",
        "bash-language-server": "^1.6.1",
        "eslint": "^6.6.0",
        "fkill-cli": "^5.2.0",
        "jsonlint": "^1.6.3",
        "prettier": "^1.19.1"
      }