You can use another NPM script to add shebang as the last part of your build process. This is not very, but it works.
"scripts": { "transpile": "babel cli.js --out-file es5.js", "shebang": "echo -e '#!/usr/bin/env/node\n' $(cat es5.js) > cli.es5.js", "prepublish": "npm run transpile && npm run shebang", }
Then your original cli.js will become
import pkg from './package' console.log(pkg.version);
The resulting es5.js file becomes
'use strict'; var _package = require('./package');
And finally, cli.es5.js becomes
#!/usr/bin/env node 'use strict'; var _package = require('./package');
This can be improved with a clean script.
"scripts": { "transpile": "babel cli.js --out-file es5.js", "shebang": "echo -e '#!/usr/bin/env/node\n' $(cat es5.js) > cli.es5.js", "clean": "rm es5.js cli.es5.js", "prepublish": "npm run clean && npm run transpile && npm run shebang", }
Of course, this requires you to be on a system with bash (or some alternative compatible shell), however you could make it cross-platform by rewriting build scripts to use node implementations of these commands with something like ShellJS .
source share