DEV Community

Discussion on: Using npx and npm scripts to Reduce the Burden of Developer Tools

Collapse
 
nebrius profile image
Bryan Hughes

Cool article! 💜 npx!

I thought I'd throw out that in certain situations, there is another solution: install the tool as a dev depedency locally and call it from a run script.

npm does this trick that I don't think many folks are aware of: whenever you install a module with a "bin" entry in it's package.json, it installs a link to that bin entry in node_modules/.bin. Then, whenever you call a run script, npm will automatically append node_modules/.bin to your path before executing any commands. The upshot is that you don't have to require your users to do a manual step, you don't have to wait for npx to download the dependency on the fly, and you can control the version of prettydiff like you would for any other dependency.

Your package.json would effectively look like this:

{
  "devDependencies": {
    "prettydiff": "^101.0.0"
  },
  "scripts": {
    "diff": "prettydiff diff source:\"app.js\" diff:\"app.complete.js\""
  }
}
Enter fullscreen mode Exit fullscreen mode

This does assume that you want this dependency tracked in package.json though, which isn't always the case, and I do think npx is a little more flexible in the end. Still a good trick though.

Collapse
 
daveskull81 profile image
dAVE Inden

This is really cool! I didn't know about this. Being able to execute the package without having to download it is pretty nice. Thanks for sharing this!

Collapse
 
bnb profile image
Tierney Cyren

I never knew about this! I've seen many projects suggest using this, but was always incredibly frustrated because I could never quite get it to work and always ended up installing globally. My assumption was that the authors were making an assumption that I already have the module globally installed.

Thanks for sharing this, Bryan! 💖