Reputation: 2426
I'm not sure what the correct vocabulary is to even search for this question, but I can mention a couple packages that implement the pattern I'm trying to pick up:
shx
cross-env
npm-run
Once you install any of those packages, they are available via CLI. After I install cross-env
as a dependency for a new project, for example, I can create an npm script in my package.json
which is like "start": "cross-env NODE_ENV=production webpack"
I inspected the package.json
files for those projects and they all use the bin
field, but if I initialize a local project (npm init
) and add a bin field it doesn't recognize it on my command line, even after running npm install
.
So how can I get that same functionality? Please also let me know if the question is clear or if I should add some other information.
My package.json
is below:
{
"name": "sloth-cli",
"version": "1.0.0",
"description": "a dead simple Chron cli tool for Node",
"bin": {
"sloth": "node ."
},
"main": "index.js",
"scripts": {
"start": "node ."
},
"repository": {
"type": "git",
"url": "git+https://github.com/Vandivier/sloth.git"
},
"keywords": [
"chron"
],
"author": "John Vandivier",
"license": "MIT",
"bugs": {
"url": "https://github.com/Vandivier/sloth/issues"
},
"homepage": "https://github.com/Vandivier/sloth#readme"
}
Upvotes: 1
Views: 207
Reputation: 2426
I eventually discovered the answer and got sloth-cli
working. bin
in package.json
is indeed key, but after defining bin we have to run npm link
on our local.
In addition, the content of bin
cannot contain typical npm script syntax. It must refer to a file which is set up to execute in a CLI context. This can be as simple as prepending #! /usr/bin/env node
at the top of a javascript file.
Once a package is published to npm, those who install it as a dependency need not run npm link
. Npm handles that part. Below is the current working package.json
for sloth-cli
:
{
"name": "sloth-cli",
"version": "1.0.2",
"description": "a dead simple Chron cli tool for Node",
"bin": {
"sloth": "./index.js"
},
"main": "index.js",
"scripts": {
"immediately": "sloth .1 \"npm run say-hello\" true true",
"start": "sloth .1 \"npm run say-hello\"",
"say-hello": "node hello.js"
},
"repository": {
"type": "git",
"url": "git+https://github.com/Vandivier/sloth.git"
},
"keywords": [
"chron"
],
"author": "John Vandivier",
"license": "MIT",
"bugs": {
"url": "https://github.com/Vandivier/sloth/issues"
},
"homepage": "https://github.com/Vandivier/sloth#readme",
"dependencies": {
"shelljs": "^0.8.1"
}
}
Upvotes: 1