node.js - 管理不同环境的 yarn 安装的最佳实践

标签 node.js yarnpkg package.json dotfiles alfred

我在 MacOS 和 Linux 上对点文件使用相同的 package.json。直到最近其中一个软件包停止在 Linux 上安装时,这才成为问题。 (失败的原因是该包需要安装 Alfred,一个 MacOS 应用程序)。当在我的 Linux 环境中找不到它时,安装就会失败。

管理此类事情的最佳方法是什么?我应该有两个不同的 package.json 文件吗?一种用于通用包,另一种用于 Mac 特定的包?

作为引用,我当前获得的依赖项是:

  "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"
  }

我在 Linux 下遇到的错误是:

$ 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.

最佳答案

我的解决方案是我决定滥用devDependency。我已将 alfred-fkill (仅限 MacOS 的 Node 模块)声明为开发依赖项。然后在 Linux 上运行 yarn install --product=true。这忽略了开发人员部门,我不再遇到安装失败的情况。在我的 Mac 上,我将运行yarn install,它将安装所有内容,就像以前一样。这可行,但很奇怪。

我剩下:

  "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"
  }

关于node.js - 管理不同环境的 yarn 安装的最佳实践,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58882496/

相关文章:

javascript - 如何启用调试 express.js/node.js 应用程序

node.js - package-lock.json 会阻止更新吗?

node.js - 使用基本身份验证重定向到另一个网站(在 Node 中)

node.js - 需要用mongodb中的条件更新字段

javascript - 如何每天创建一个新日志

node.js - 将 node-gyp 标志传递给包时, yarn add package --build-from-source 的行为是否类似于 npm install package --build-from-source?

node.js - 如何在 package.json 的 webpack 中添加 NODE_PATH?

javascript - 需要 "yarn/npm link"工作流来开发和发布 "multiple copies of react"模块

reactjs - 在 CI 管道上构建项目以及尝试通过 Yarn 添加包时出现错误 "error Couldn' t find package "XXX"

docker-compose 无法以 npm ERR 启动! enoent ENOENT : no such file or directory, 打开 '/usr/src/app/package.json'