Skip to content

Instantly share code, notes, and snippets.

@andreineculau
Created July 23, 2020 22:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andreineculau/03d7abb473f816dc6d10087ff6f46766 to your computer and use it in GitHub Desktop.
Save andreineculau/03d7abb473f816dc6d10087ff6f46766 to your computer and use it in GitHub Desktop.
How to run a node script that has a package dependency
# Assuming that 2.js is executable
# chmod +x ./2.js
npx -p lodash ./2.js
#!/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