🦵

Zenn CLI導入でつまづいたことメモ

2023/04/21に公開

この度、Zennでの執筆を始めました。

普段は個人ブログで執筆しているのですが、せっかくだったらGitHubの草も生やして可視化したいなーということでリポジトリ管理することにしました。

それに伴い、Zenn CLIの導入手順についてメモです。

導入手順

  1. GitHubにZenn用のリポジトリを作成(例:zenn-posts)
  2. Zennと上記のリポジトリを連携する
  3. ローカルにクローンしてくる
  4. ローカルリポジトリ内でCLIを導入するコマンドを実行する

npm init --yes

ここはOK(package.jsonが生成される)

npm install zenn-cli

ここもOK(node_modules/と、package-lock.jsonが生成)

npx zenn init

SyntaxError: Unexpected token '?'

ここでエラーが。本来であればarticlesディレクトリが作成されるはずなのに、ない。

CLI導入できない原因

こちらの記事によると、Node.jsのバージョンが関係しているっぽい

https://zenn.dev/dai_otsuka/articles/f293ec144fe0bf

確かに、確認してみるとめっちゃ古かった。面倒なので入れ直す。以下の記事を参考にした。

$ node -v
v12.11.1
$ node --version
v12.11.1
$ npm uninstall -g npm
removed 254 packages, and audited 1 package in 835ms
found 0 vulnerabilities

$ rm -rf .npm \
> node_modules

$ brew uninstall node
Uninstalling /usr/local/Cellar/node/17.6.0... (2,016 files, 46.0MB)

$ brew install nodebrew
Error:
  homebrew-core is a shallow clone.
To `brew update`, first run:
  git -C /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core fetch --unshallow
This command may take a few minutes to run due to the large size of the repository.
This restriction has been made on GitHub's request because updating shallow
clones is an extremely expensive operation due to the tree layout and traffic of
Homebrew/homebrew-core and Homebrew/homebrew-cask. We don't do this for you
automatically to avoid repeatedly performing an expensive unshallow operation in
CI systems (which should instead be fixed to not use shallow clones). Sorry for
the inconvenience!
Warning: nodebrew 1.1.0 is already installed and up-to-date.
To reinstall 1.1.0, run:
  brew reinstall nodebrew

$ nodebrew -v
nodebrew 1.1.0

Usage:
    nodebrew help                         Show this message
    nodebrew install <version>            Download and install <version> (from binary)
    nodebrew compile <version>            Download and install <version> (from source)
    nodebrew install-binary <version>     Alias of `install` (For backward compatibility)
    nodebrew uninstall <version>          Uninstall <version>
    nodebrew use <version>                Use <version>
    nodebrew list                         List installed versions
    nodebrew ls                           Alias for `list`
    nodebrew ls-remote                    List remote versions
    nodebrew ls-all                       List remote and installed versions
    nodebrew alias <key> <value>          Set alias
    nodebrew unalias <key>                Remove alias
    nodebrew clean <version> | all        Remove source file
    nodebrew selfupdate                   Update nodebrew
    nodebrew migrate-package <version>    Install global NPM packages contained in <version> to current version
    nodebrew exec <version> -- <command>  Execute <command> using specified <version>

Example:
    # install
    nodebrew install v8.9.4

    # use a specific version number
    nodebrew use v8.9.4

$ nodebrew ls-remote
# 省略
v19.0.0   v19.0.1   v19.1.0   v19.2.0   v19.3.0   v19.4.0   v19.5.0   v19.6.0
v19.6.1   v19.7.0   v19.8.0   v19.8.1   v19.9.0

v20.0.0
# 省略

$ nodebrew install-binary stable
Fetching: https://nodejs.org/dist/v20.0.0/node-v20.0.0-darwin-x64.tar.gz
######################################################################################################################################################################################################## 100.0%
Installed successfully

$ nodebrew ls
v12.11.1
v20.0.0

current: v12.11.1

$ nodebrew use v20.0.0
use v20.0.0

$ nodebrew ls
v12.11.1
v20.0.0

current: v20.0.0

$ node -v
v20.0.0

$ npx zenn init
npm ERR! code ENOVERSIONS
npm ERR! No versions available for zenn

npm ERR! A complete log of this run can be found in: /Users/yutarom/.npm/_logs/2023-04-20T14_36_28_371Z-debug-0.log

$ npm init --yes #やり直した
Wrote to /Users/yutarom/Desktop/repository/zenn-posts/package.json:

{
  "name": "zenn-posts",
  "version": "1.0.0",
  "description": "Zennに投稿してる記事たち",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/yuutarou22/zenn-posts.git"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "bugs": {
    "url": "https://github.com/yuutarou22/zenn-posts/issues"
  },
  "homepage": "https://github.com/yuutarou22/zenn-posts#readme",
  "dependencies": {
    "zenn-cli": "^0.1.142"
  }
}

$ npm install zenn-cli

added 1 package, and audited 2 packages in 1s

found 0 vulnerabilities

$ npx zenn init
Generating README.md skipped.

  🎉  Done!
  早速コンテンツを作成しましょう

  👇  新しい記事を作成する
  $ zenn new:article

  👇  新しい本を作成する
  $ zenn new:book

  👇  投稿をプレビューする
  $ zenn preview

$ zenn new:article
-bash: zenn: command not found

$ ls
README.md          articles/          books/             node_modules/      package-lock.json  package.json

$ npm install zenn-cli@latest
up to date, audited 2 packages in 383ms

$ npx zenn new:article # 記事作成
created: articles/0c320a14163b7e.md

参考ページ

https://zenn.dev/zenn/articles/install-zenn-cli

GitHubで編集を提案

Discussion