简体   繁体   English

使用npm和yarn的package.json脚本?

[英]package.json scripts that work with npm and yarn?

I am using npm as a build tool and so in my package.json , and some of my scripts depend on other scripts: 我使用npm作为构建工具 ,所以在我的package.json ,我的一些脚本依赖于其他脚本:

{
  "test": "npm run lint && mocha"
}

This hardcodes the npm package manager into package.json . 这将npm包管理器硬编码到package.json How can make this approach to expressing dependencies work with both npm and yarn ? 如何使这种表达依赖关系的方法适用于npmyarn

The $npm_execpath environment variable refers to the build tool, so just replace npm with the $npm_execpath : $npm_execpath环境变量引用构建工具,所以只需用$npm_execpath替换npm

{
  "test": "$npm_execpath run lint && mocha"
}

Both npm test and yarn test will work, and will use the appropriate build tool. npm testyarn test都可以使用,并将使用适当的构建工具。

While mjs' answer is great, there's also a small package that is purported to work on all environments including Windows: https://www.npmjs.com/package/yarpm 虽然mjs的答案很棒,但还有一个小包装,据称适用于所有环境,包括Windows: https//www.npmjs.com/package/yarpm

To use in a project, run yarn add yarpm --dev / npm i -D yarpm and then just use yarpm in your scripts like this: 要在项目中使用,请运行yarn add yarpm --dev / npm i -D yarpm ,然后在脚本中使用yarpm,如下所示:

{
  "test": "yarpm run lint && mocha"
}

As the package README notes, you just need to make sure your commands would be suitable for passing through to either yarn or npm: you cannot use arguments/flags that only work on one package manager. 由于包自述文件说明,你只需要确保你的命令,将适合于经过要么纱线或NPM:不能使用参数/标志,只有在一个包管理工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM