【问题标题】:Does yarn add package --build-from-source behave like npm install package --build-from-source when passing node-gyp flags to packages?将 node-gyp 标志传递给包时, yarn add package --build-from-source 的行为是否类似于 npm install package --build-from-source?
【发布时间】:2018-01-27 05:55:59
【问题描述】:

看起来 yarn 不像 npm 那样将 node-gyp 标志传递给本机包。

例如,当尝试安装 sqlite3@3.1.6 时:

npm install sqlite3@3.1.6 \
  --build-from-source \
  --sqlite_libname=sqlcipher \
  --sqlite=`brew --prefix` \
  --verbose

我们成功安装了带有 sqlcipher 扩展的 sqlite3,因为在 sqlite3 的 binding.gyp 中传递了 --sqlite_libname--sqlite,它们是 specified

但是,当尝试使用 yarn 并运行我认为等效的命令时,标志似乎没有得到尊重:

yarn add sqlite3@3.1.6 \
  --force \
  --build-from-source \
  --sqlite_libname=sqlcipher \
  --sqlite=`brew --prefix` \
  --verbose

使用 npm 无法识别的命令行参数将转换为 gyp 标志。

yarn 似乎不起作用。

有没有办法通过yarn 获得此功能?

【问题讨论】:

  • 是否有任何答案满足您的问题?

标签: node.js npm node-gyp yarnpkg


【解决方案1】:

这目前可以通过使用npm_config_{snake_case_param}=true/false格式的环境变量来实现

例如,npm install --build-from-source=true 变为:

npm_config_build_from_source=true yarn install

这里有记录 https://yarnpkg.com/lang/en/docs/envvars/#toc-npm-config

【讨论】:

    【解决方案2】:

    Yarn 不会自动将 install 命令的 -- 参数暴露给生命周期脚本(依赖项的 package.json 中的 pre/post/install 脚本)。 这是 Yarn 为脚本执行构建 Env 的代码https://github.com/yarnpkg/yarn/blob/master/src/util/execute-lifecycle-script.js#L39

    您可以通过 .yarnrc 中的 env 设置传递特定值,它还可以基于 .yarnrc/.npmrc 配置构建 npm_config_* 设置。

    【讨论】:

    • 你能举个例子吗? .yarnrc 文件在 yarn 的网站上似乎没有很好地记录。
    • .yarnrc 对 yarn.lock 文件使用相同的解析器,与 yaml 文件非常相似。您可以在 .yarnrc env 中添加一个 env 部分: ENV_PARAM "value" ENV_PARAM_2 "value2" 试一试,如果可行,请贡献给文档,其中大部分是由社区完成的,非常感谢yarnpkg.com/en/docs/yarnrc
    • 但据我所知,这些都不是环境变量。它们是传递给安装脚本的命令行参数。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-01
    • 2013-09-06
    • 2021-01-13
    • 2018-07-28
    • 2022-12-02
    相关资源
    最近更新 更多