package.json scripts that work with npm and yarn?

若如初见. 提交于 2020-01-22 13:48:33

问题


I am using npm as a build tool and so in my package.json, and some of my scripts depend on other scripts:

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

This hardcodes the npm package manager into package.json. How can make this approach to expressing dependencies work with both npm and yarn?


回答1:


The $npm_execpath environment variable refers to the build tool, so just replace npm with the $npm_execpath:

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

Both npm test and yarn test will work, and will use the appropriate build tool.




回答2:


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

To use in a project, run yarn add yarpm --dev / npm i -D yarpm and then just use yarpm in your scripts like this:

{
  "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.



来源:https://stackoverflow.com/questions/41647961/package-json-scripts-that-work-with-npm-and-yarn

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!