29
loading...
This website collects cookies to deliver better user experience
npx
- which can be used to run Node.js scripts hosted in npm packages or at any other URL. Even if not already installed, it will download the package into a cache to execute the files.create-react-app
and create-nuxt-app
)nodemon
, tailwindcss
, and ngrok
)workin-hard
, cowsay
)git clone https://github.com/deepgram-devs/npm-package
cd npm-package
npm install
bin.js
.package.json
.bin.js
file with a 'shebang'.bin.js
file in your project, open package.json
, and add a new bin
property:{
"name": "@username/first-package",
"version": "0.0.3",
"dependencies": {
"axios": "^0.24.0"
},
"bin": "./bin.js"
}
bin.js
in your code editor, make the first line a Node shebang, and then create a basic script that will run when the file is executed:#!/usr/bin/env node
console.log('Hello world!')
npx .
, and you should see Hello world! printed.process.args
- try updating the bin.js
file:#!/usr/bin/env node
console.log(process.args)
npx . hello world
and you should see something like this:[
'/Users/kevin/.nvm/versions/node/v16.13.0/bin/node',
'/Users/kevin/.npm/_npx/0b61241d7c17bcbb/node_modules/.bin/first-package',
'hello',
'world'
]
node
installation and first-package
. Then, hello
and world
are included.yargs
. Install it from your terminal with npm install yargs
and update your bin.js
file:#!/usr/bin/env node
const yargs = require('yargs')
console.log(yargs.argv)
npx . --capitalize --phrase "Hello World" extra args
{
capitalize: true,
phrase: 'Hello World',
_: ['extra', 'args']
}
bin.js
file.index.js
exports a class that expects an apiKey
value when initialized. It has one member method - get(parameters)
- that takes in an object with properties with which to call The Open Movie Database API.bin.js
:#!/usr/bin/env node
const yargs = require('yargs')
const OpenMovieDatabase = require('./index')
const omdb = new OpenMovieDatabase(yargs.argv.key)
if(yargs.argv.title) {
omdb.get({ t: yargs.argv.title }).then(results => {
console.log(results)
})
}
if(yargs.argv.search) {
omdb.get({ s: yargs.argv.search }).then(results => {
console.log(results.Search)
})
}
npx . --key=your_api_key --title "Zombieland"
npx . --key=your_api_key --search "Spiderman"
t
or s
, you allow the user to provide the title
or search
arguments.key
argument and either a title
argument or a search
argument. You will also want to restrict the user from providing both as that will lead to two logs which doesn't look great. Thanks to yargs
, you already know if arguments have been provided, so some boolean logic is all that's needed.omdb
is declared, add the following checks:if(!yargs.argv.key) {
return console.log('You must provide a key argument with an OMDb API Key')
}
if(!yargs.argv.title && !yargs.argv.search) {
return console.log('You must provide either a title or search argument - you have provided neither')
}
if(yargs.argv.title && yargs.argv.search) {
return console.log('You must provide either a title or search argument - not both')
}
key
, omit title
and search
, or provide both title
and search
.package.json
and then run npm publish
from your terminal.npx @username/first-package --key=your_api_key --title "Zombieland"
.npx @phazonoverload/first-package --key=your_api_key --title "Zombieland"
.