-
-
Save andreineculau/03d7abb473f816dc6d10087ff6f46766 to your computer and use it in GitHub Desktop.
How to run a node script that has a package dependency
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Assuming that 2.js is executable | |
# chmod +x ./2.js | |
npx -p lodash ./2.js |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
// access the npx node_modules required via -p | |
module.paths.unshift(process.env.PATH.split(':')[0].replace(/\/bin$/, '/lib/node_modules')); | |
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | |
let _ = require('lodash'); | |
console.log(_.toUpper('lodash ftw!')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment