How to make npx work with my custom npm pakage?
Asked Answered
M

1

6

I've published a new package which aims to generate a very small boilerplate for a node open source project via cli. For now, it's just a combination of few npx commands and requires other npm packages like gitignore, license to work. I want to execute the build script in package.json with the following command.

npx get-set-node-oss build [email protected]

Link to the npm package: get-set-node-oss. I know the name is a bit too long.

{
  "name": "get-set-node-oss",
  "version": "1.0.1",
  "description": "One command setup for your Node OSS project",
  "scripts": {
    "build": "npx license mit > LICENSE && npx gitignore node && npx covgen"
  },
  "author": "Harshit Juneja",
  "license": "MIT",
  "repository": {
    "type": "git",
    "url": "git+https://github.com/harshitjuneja/get-set-node-oss.git"
  },
  "keywords": [
    "node",
    "boilerplate","gitignore","MIT","OSS"
  ],
  "bugs": {
    "url": "https://github.com/harshitjuneja/get-set-node-oss/issues"
  },
  "homepage": "https://github.com/harshitjuneja/get-set-node-oss#readme"
}

I expect the user to make a new folder and cd into the folder and do

npx get-set-node-oss build emailstring

and get the resulting boilerplate files.

Mcduffie answered 9/1, 2019 at 9:2 Comment(1)
Is it recommended to include an entry point, even in case it doesn't serve any purpose(as in my case)?Mcduffie
A
3

For npx to work you need to specify what the get-set-node-oss command means. Thankfully, this can be accomplished by using the bin field in your package.json file. More information from NPM documentation for your reference:

To use this, supply a bin field in your package.json which is a map of command name to local file name. On install, npm will symlink that file into prefix/bin for global installs, or ./node_modules/.bin/ for local installs.

An example of it below.

"bin": {   
    "get-set-node-oss": //script you like to run
},

Hope it works out for you. I have understood bin as scripts in package.json. Where we can either do npm run foo or npx foo both yielding the same results. If you want an example, here you go https://github.com/vipulgupta2048/balenaclone

Alage answered 30/11, 2020 at 22:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.