7
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Canva CLI(公式)触ってCanvaアプリ入門してみた

Posted at

canvaコマンド、便利そうですね。地元で仕事の人にあったようなモヤモヤ感があります。

サードパーティかと思ったら公式で出ています。

CLIからスライド作るツールではなくCanvaアプリを作るツール

Canvaアプリを作るとCanva内でボタンを押して何かするなどができそうでした。

みたいですね。

npmのページはこちら

@canva/cli is a command line tool designed for creating and managing Canva Apps. Use @canva/cli to get started creating and testing your app. The Canva CLI allows you to create apps from the command line, and to use Canva's recommended development tools and templates.

To learn more about app development, visit the official documentation.

Canvaアプリの作成と管理できる模様

こちらの投稿で発見

試していく

Node.js V20系で試したらうまくいきましたが、22や24などだとエラーが出て厳しかったです。

インストール

npm install -g @canva/cli@latest

ログイン

canvaコマンドが使えます。

$ canva login

ブラウザが開いてOAuth認証です。

CleanShot 2025-07-24 at 15.06.05.png

認証コードが表示さるのでコピー

CleanShot 2025-07-24 at 15.08.53.png

ターミナル側に貼り付け

CleanShot 2025-07-24 at 15.08.34.png

この状態でエンター。タターン

CleanShot 2025-07-24 at 15.09.26.png

ログインできました。めちゃめちゃClaude CodeやGemini CLIぽい。

CleanShot 2025-07-24 at 15.09.49.png

試しにアプリを作ってみる

適当なフォルダで以下を実行します。

$ canva apps create "My New App" --template="hello_world" --distribution="public" --git --installDependencies

CleanShot 2025-07-24 at 15.12.11.png

思ったより時間かかりました。 90%からが進みが悪い。

CleanShot 2025-07-24 at 15.13.24.png

プロジェクト作成成功。フォルダ移動して実行せよとのことなので実行します。

$ cd my-new-app
$ npm start

ビルドされてますね

CleanShot 2025-07-24 at 15.14.21.png

CleanShot 2025-07-24 at 15.15.15.png

ここにアプリが表示される...らしい

CleanShot 2025-07-24 at 15.49.51.png

デフォルトで試しただけだけどエラーが。。。

CleanShot 2025-07-24 at 15.57.24.png

Node.js v20でやった人はうまくいくはず

補足: v22や24で試したらエラーでした

npm run startでのサーバー起動もwebpackのエラーっぽい感じでした。
npm run buildも同様

$ npm run build
> build
> webpack --config webpack.config.ts --mode production && npm run extract
[webpack-cli] Failed to load '/Users/n0bisuke/ds/2_playground/my-new-app/qr-generator/webpack.config.ts' config
[webpack-cli] SyntaxError: The requested module 'webpack' does not provide an export named 'DefinePlugin'
    at ModuleJobSync.runSync (node:internal/modules/esm/module_job:466:37)
    at ModuleLoader.importSyncForRequire (node:internal/modules/esm/loader:435:47)
    at loadESMFromCJS (node:internal/modules/cjs/loader:1565:24)
    at Module._compile (node:internal/modules/cjs/loader:1716:5)
    at Object.loadTS [as .ts] (node:internal/modules/cjs/loader:1826:10)
    at Module.load (node:internal/modules/cjs/loader:1469:32)
    at Module._load (node:internal/modules/cjs/loader:1286:12)
    at TracingChannel.traceSync (node:diagnostics_channel:322:14)
    at wrapModuleLoad (node:internal/modules/cjs/loader:235:24)
    at Module.require (node:internal/modules/cjs/loader:1491:12)

Node.jsをv20 && npm run build

Node.js v20だとnpm startで開発サーバー起動もトラブってました。

v20系で試してnpm run buildをするとdistフォルダができ、その中のjsファイルを使ってうまくいきました。

$ npm run build
  • distフォルダが作成されてjsファイルが

CleanShot 2025-07-25 at 17.38.52.png

生成されたJSファイルをDevelopersサイトで登録します。

  • Developersサイトへ

CleanShot 2025-07-25 at 17.38.32.png

  • アプリのソースの項目でアップロード

CleanShot 2025-07-25 at 17.38.43.png

動いた!

image.png

ボタンを押すと、Hello worldの文字がスライドに出力されました。なるほどこういうサンプルだったんですね。

ムスカの名前を入力

テンプレートは以下のにようなsrc/app.tsxを書き方えたら良いみたいですね。

import { Button, Rows, Text } from "@canva/app-ui-kit";
import { requestOpenExternalUrl } from "@canva/platform";
import { FormattedMessage, useIntl } from "react-intl";
import { useAddElement } from "utils/use_add_element";
import * as styles from "styles/components.css";

export const DOCS_URL = "https://www.canva.dev/docs/apps/";

export const App = () => {
  const addElement = useAddElement();

  const onClick = () => {
    addElement({
      type: "text",
      children: ["Hello world!"],
    });
  };

  const openExternalUrl = async (url: string) => {
    const response = await requestOpenExternalUrl({
      url,
    });

    if (response.status === "aborted") {
      // user decided not to navigate to the link
    }
  };

  const intl = useIntl();

  return (
    <div className={styles.scrollContainer}>
      <Rows spacing="2u">
        <Text>
          <FormattedMessage
            defaultMessage="
              To make changes to this app, edit the <code>src/app.tsx</code> file,
              then close and reopen the app in the editor to preview the changes.
            "
            description="Instructions for how to make changes to the app. Do not translate <code>src/app.tsx</code>."
            values={{
              code: (chunks) => <code>{chunks}</code>,
            }}
          />
        </Text>
        <Button variant="primary" onClick={onClick} stretch>
          {intl.formatMessage({
            defaultMessage: "Do something cool",
            description:
              "Button text to do something cool. Creates a new text element when pressed.",
          })}
        </Button>
        <Button variant="secondary" onClick={() => openExternalUrl(DOCS_URL)}>
          {intl.formatMessage({
            defaultMessage: "Open Canva Apps SDK docs",
            description:
              "Button text to open Canva Apps SDK docs. Opens an external URL when pressed.",
          })}
        </Button>
      </Rows>
    </div>
  );
};

  const onClick = () => {
    addElement({
      type: "text",
      children: ["私の名前は、ロムスカ・パロ・ウル・ラピュタ"],
    });
  };

こんな感じです。

まとめ

Canvaアプリ、ストアにリリースもできそうなので色々と作って誰かに使ってもらうもできそうですね。

CanvaCLIはアプリ作る時に使う模様ですが、Node.js v22などでバグるのがちょっと気になりますね。。。

まず試すにはNode.js v20系でやるのがオススメです。

7
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?