Aeria Leeve Says

Make `npm init` work for a package

npm init example

To make npm init example work, first a package called create-example must exist. Secondly in package.json, bin must point to an executable OR bin.create-example must point to an executable OR directories.bin must point to an directory with an executable called create-example.

To see this in action (assuming bash):

# Create a directory for the demo
mkdir /tmp/npm-init-example
cd /tmp/npm-init-example

# We are going to demo using npm workspace which needs a root `package.json` file
npm init --yes

# Create a package in the workspace. It must be called `create-example` because
# `name` in package.json has to be `create-example`
npm init --yes --workspace packages/create-example

# Create an executable. Name it `create-example` so it works with `directories.bin` setup
mkdir -p packages/create-example/bin
cat <<'SH' > packages/create-example/bin/create-example
#!/usr/bin/env sh
echo NPM INIT EXAMPLE
SH
chmod u+x packages/create-example/bin/create-example

# Set `bin`, `bin.create-example` or `directories.bin` in `package.json`
npm --workspace create-example pkg set bin=./bin/create-example
#npm --workspace create-example pkg set bin.create-example=./bin/create-example
#npm --workspace create-example pkg set directories.bin=./bin

# Make npm add node_modules/.bin. Two `npm install`s are required.
npm install
npm install

# We should see `NPM INIT EXAMPLE`
npm init example

npm init @example

To make npm init @example work, a package called @example/create must exist.

# Create a directory for the demo
mkdir /tmp/npm-init-at-example
cd /tmp/npm-init-at-example

# We are going to demo using npm workspace which needs a root `package.json` file
npm init --yes

# Create a package in the workspace
npm init --yes --workspace packages/create --scope @example

# Create an executable. Name it `create` so it works with `directories.bin` setup
mkdir -p packages/create/bin
cat <<'SH' > packages/create/bin/create
#!/usr/bin/env sh
echo NPM INIT @EXAMPLE
SH
chmod u+x packages/create/bin/create

# Set `bin`, `bin.create` or `directories.bin` in `package.json`
#npm --workspace @example/create pkg set bin=./bin/create
npm --workspace @example/create pkg set bin.create=./bin/create
#npm --workspace @example/create pkg set directories.bin=./bin

# Make npm add node_modules/.bin. Two `npm install`s are required.
npm install
npm install

# We should see `NPM INIT @EXAMPLE`
npm init @example

npm init @example/something

To make npm init @example/something work, a package called @example/create-something must exist.

# Create a directory for the demo
mkdir /tmp/npm-init-at-example-something
cd /tmp/npm-init-at-example-something

# We are going to demo using npm workspace which needs a root `package.json` file
npm init --yes

# Create a package in the workspace
npm init --yes --workspace packages/create-something --scope @example

# Create an executable. Name it `create-something` so it works with `directories.bin` setup
mkdir -p packages/create-something/bin
cat <<'SH' > packages/create-something/bin/create-something
#!/usr/bin/env sh
echo NPM INIT @EXAMPLE/SOMETHING
SH
chmod u+x packages/create-something/bin/create-something

# Set `bin`, `bin.create` or `directories.bin` in `package.json`
#npm --workspace @example/create-something pkg set bin=./bin/create
#npm --workspace @example/create-something pkg set bin.create=./bin/create
npm --workspace @example/create-something pkg set directories.bin=./bin

# Make npm add node_modules/.bin. Two `npm install`s are required.
npm install
npm install

# We should see `NPM INIT @EXAMPLE/SOMETHING`
npm init @example/something