今回は、
npm-init
からです。

 

npm-init
 

Create a package.json file

package.jsonファイルを作成する

 

Synopsis

npm init <package-spec> (same as `npx <package-spec>`)
npm init <@scope> (same as `npx <@scope>/create`)

aliases: create, innit

 

npm init <initializer> can be used to set up a new or existing npm package.

npm init<initializer>は、新規または既存のnpmパッケージを設定するために使用できる。

 

initializer in this case is an npm package named create-<initializer>, which will be installed by npm-exec, and then have its main bin executed -- presumably creating or updating package.json and running any other initialization-related operations.

この場合の初期化子はcreate-<initializer>という名前のnpmパッケージで、npm-execによってインストールされ、メインのbinが実行されます--おそらくpackage.jsonを作成または更新し、その他の初期化関連の操作を実行します。

 

The init command is transformed to a corresponding npm exec operation as follows:

initコマンドは、次のように対応するnpm exec操作に変換されます。

 

    ・npm init foo -> npm exec create-foo
    ・npm init @usr/foo -> npm exec @usr/create-foo
    ・npm init @usr -> npm exec @usr/create
    ・npm init @usr@2.0.0 -> npm exec @usr/create@2.0.0
    ・npm init @usr/foo@2.0.0 -> npm exec @usr/create-foo@2.0.0

 

If the initializer is omitted (by just calling npm init), init will fall back to legacy init behavior. It will ask you a bunch of questions, and then write a package.json for you. It will attempt to make reasonable guesses based on existing fields, dependencies, and options selected. It is strictly additive, so it will keep any fields and values that were already set. You can also use -y/--yes to skip the questionnaire altogether. If you pass --scope, it will create a scoped package.

イニシャライザが省略された場合(npm initを呼び出すだけで)、initはレガシーなinit動作に戻る。たくさんの質問をしてから、package.jsonを作成してくれる。既存のフィールド、依存関係、および選択されたオプションに基づいて、合理的な推測を行おうとする。これは厳密に加法的であるため、すでに設定されているフィールドと値はすべて保持される。また、-y/--yesを使用して、アンケートを完全にスキップすることもできる。--scopeを渡すと、スコープされたパッケージが作成される。

 

Note: if a user already has the create-<initializer> package globally installed, that will be what npm init uses. If you want npm to use the latest version, or another specific version you must specify it:

注意:ユーザーがすでにcreate-<initializer>パッケージをグローバルにインストールしている場合、npm initはこれを使用します。npmに最新バージョンまたは別の特定のバージョンを使用させたい場合は、次のように指定する必要があります。

 

・npm init foo@latest # fetches and runs the latest create-foo from the registry

レジストリから最新のcreate-fooを取得して実行します。

 

・npm init foo@1.2.3 # runs create-foo@1.2.3 specifically

具体的にcreate-foo@1.2.3を実行します。

 

Forwarding additional options

転送の追加オプション

Any additional options will be passed directly to the command, so npm init foo -- --hello will map to npm exec -- create-foo --hello.

追加のオプションはコマンドに直接渡されるので、npm init foo----helloはnpm exec--create-foo--helloにマップされます。

 

To better illustrate how options are forwarded, here's a more evolved example showing options passed to both the npm cli and a create package, both following commands are equivalent:

オプションがどのように転送されるかをよりよく説明するために、npm cliとcreate packageの両方に渡されるオプションを示す、より進化した例を次に示します。次の両方のコマンドは同等です。

 

    ・npm init foo -y --registry=<url> -- --hello -a
    ・npm exec -y --registry=<url> -- create-foo --hello -a

 

Examples

 

Create a new React-based project using create-react-app:

create-react-appを使用して、新しいReactベースのプロジェクトを作成します。

 

$ npm init react-app ./my-react-app

 

Create a new esm-compatible package using create-esm:

create-esmを使用して、新しいesm互換パッケージを作成します。

 

$ mkdir my-esm-lib && cd my-esm-lib
$ npm init esm --yes

 

Generate a plain old package.json using legacy init:

レガシーinitを使用して、プレーンな古いpackage.jsonを生成します。

 

$ mkdir my-npm-pkg && cd my-npm-pkg
$ git init
$ npm init

 

Generate it without having it ask any questions:

質問をしないで生成します。

 

$ npm init -y

 

Workspaces support

 

It's possible to create a new workspace within your project by using the workspace config option. When using npm init -w <dir> the cli will create the folders and boilerplate expected while also adding a reference to your project package.json "workspaces": [] property in order to make sure that new generated workspace is properly set up as such.

ワークスペース設定オプションを使用して、プロジェクト内に新しいワークスペースを作成することができます。npm init-w<dir>を使用すると、cliは期待されるフォルダとボイラープレートを作成すると同時に、プロジェクトpackage.json"workspaces":[]プロパティへの参照を追加して、新しく生成されたワークスペースが適切に設定されていることを確認します。

 

Given a project with no workspaces, e.g:

ワークスペースのないプロジェクトを例に挙げます。

 

.
+-- package.json

 

You may generate a new workspace using the legacy init:

従来のinitを使用して新しいワークスペースを生成できます。

 

$ npm init -w packages/a

 

That will generate a new folder and package.json file, while also updating your top-level package.json to add the reference to this new workspace:

これにより、新しいフォルダとpackage.jsonファイルが生成されると同時に、トップレベルのpackage.jsonが更新され、この新しいワークスペースへの参照が追加される。

 

.
+-- package.json
`-- packages
   `-- a
       `-- package.json

 

The workspaces init also supports the npm init <initializer> -w <dir> syntax, following the same set of rules explained earlier in the initial Description section of this page. Similar to the previous example of creating a new React-based project using create-react-app, the following syntax will make sure to create the new react app as a nested workspace within your project and configure your package.json to recognize it as such:

ワークスペースinitは、npm init<initializer>-w<dir>構文もサポートしており、このページの最初の説明セクションで説明したのと同じルールセットに従っている。create-react-appを使用して新しいReactベースのプロジェクトを作成する前の例と同様に、次の構文では、新しいreactアプリをプロジェクト内にネストされたワークスペースとして作成し、package.jsonをそのように認識するように設定する。

 

npm init -w packages/my-react-app react-app .

 

This will make sure to generate your react app as expected, one important consideration to have in mind is that npm exec is going to be run in the context of the newly created folder for that workspace, and that's the reason why in this example the initializer uses the initializer name followed with a dot to represent the current directory in that context, e.g: react-app .:

これにより、Reactアプリが期待通りに生成されるようになります。覚えておくべき重要な考慮事項の1つは、npm execがそのワークスペース用に新しく作成されたフォルダのコンテキストで実行されることです。このため、この例では、初期化子は初期化子名の後にドットを付けて、そのコンテキスト内の現在のディレクトリを表します。例:react-app。

 

.
+-- package.json
`-- packages
   +-- a
   |   `-- package.json
   `-- my-react-app
       +-- README
       +-- package.json
       `-- ...

 

Configuration
yes

 

    ・Default: null
    ・Type: null or Boolean

 

Automatically answer "yes" to any prompts that npm might print on the command line.

npmがコマンド行に出力するプロンプトに対して、自動的に「yes」と応答します。
 

force

    ・Default: false
    ・Type: Boolean

 

Removes various protections against unfortunate side effects, common mistakes, unnecessary performance degradation, and malicious input.

不運な副作用、よくある間違い、不必要なパフォーマンスの低下、悪意のある入力に対するさまざまな保護を排除する。

 

・Allow clobbering non-npm files in global installs.

グローバル・インストールで非npmファイルの破壊を許可します。

 

・Allow the npm version command to work on an unclean git repository.

クリーンでないgitリポジトリーでnpm versionコマンドが動作するようにします。

 

・Allow deleting the cache folder with npm cache clean.

npm cache cleanを使用してキャッシュフォルダを削除できるようにする。

 

・Allow installing packages that have an engines declaration requiring a different version of npm.

異なるバージョンのnpmを必要とするエンジン宣言を持つパッケージのインストールを許可します。

 

・Allow installing packages that have an engines declaration requiring a different version of node, even if --engine-strict is enabled.

--engine-strictが有効になっている場合でも、異なるバージョンのノードを必要とするengines宣言を持つパッケージのインストールを許可します。

 

・Allow npm audit fix to install modules outside your stated dependency range (including SemVer-major changes).

npm監査修正が、指定された依存関係の範囲外(SemVerの主要な変更を含む)にモジュールをインストールできるようにします。

 

・Allow unpublishing all versions of a published package.

公開されたパッケージのすべてのバージョンの公開解除を許可する。

 

・Allow conflicting peerDependencies to be installed in the root project.

ルートプロジェクトに矛盾するpeerDependenciesをインストールできるようにします。

 

・Implicitly set --yes during npm init.

npm init中に暗黙的に--yesに設定されます。

 

・Allow clobbering existing values in npm pkg

npm pkgの既存の値の破壊を許可する

 

・Allow unpublishing of entire packages (not just a single version).

(単一のバージョンだけでなく)パッケージ全体のアンパブリッシュを許可します。

 

If you don't have a clear idea of what you want to do, it is strongly recommended that you do not use this option!

何をしたいのか明確な考えがない場合は、このオプションを使用しないことを強くお勧めします。

 

scope

    ・Default: the scope of the current project, if any, or ""
    ・Type: String

 

Associate an operation with a scope for a scoped registry.

操作をスコープ付きレジストリのスコープに関連付けます。

 

Useful when logging in to or out of a private registry:

プライベートレジストリにログインするとき、またはプライベートレジストリからログアウトするときに便利です。

 

# log in, linking the scope to the custom registry
npm login --scope=@mycorp --registry=https://registry.mycorp.com

# log out, removing the link and the auth token
npm logout --scope=@mycorp

 

This will cause @mycorp to be mapped to the registry for future installation of packages specified according to the pattern @mycorp/package.

これにより、パターン@mycorp/packageに従って指定されたパッケージを今後インストールするために、@mycorpがレジストリにマッピングされます。

 

This will also cause npm init to create a scoped package.

これにより、npm initはスコープ付きパッケージを作成します。

 

# accept all defaults, and create a package named "@foo/whatever",
# instead of just named "whatever"
npm init --scope=@foo --yes

 

workspace

    ・Default:
    ・Type: String (can be set multiple times)

Enable running a command in the context of the configured workspaces of the current project while filtering by running only the workspaces defined by this configuration option.

このコンフィギュレーションオプションで定義されたワークスペースのみを実行してフィルタリングしながら、現在のプロジェクトの設定済みワークスペースのコンテキストでコマンドを実行できるようにします。

 

Valid values for the workspace config are either:

ワークスペース設定の有効な値は、次のいずれかです。

 

・Path to a workspace directory
・Path to a parent workspace directory (will result in selecting all workspaces within that folder)

 

When set for the npm init command, this may be set to the folder of a workspace which does not yet exist, to create the folder and set it up as a brand new workspace within the project.

npm initコマンドに設定すると、まだ存在していないワークスペースのフォルダに設定され、フォルダが作成され、プロジェクト内の新しいワークスペースとして設定されます。

 

This value is not exported to the environment for child processes.

この値は子プロセスの環境にはエクスポートされません。

 

workspaces

    ・Default: null
    ・Type: null or Boolean

 

Set to true to run the command in the context of all configured workspaces.

trueに設定すると、設定されているすべてのワークスペースのコンテキストでコマンドが実行されます。

 

Explicitly setting this to false will cause commands like install to ignore workspaces altogether. When not set explicitly:

これを明示的にfalseに設定すると、installなどのコマンドはワークスペースを完全に無視します。明示的に設定しない場合:

 

・    Commands that operate on the node_modules tree (install, update, etc.) will link workspaces into the node_modules folder. - Commands that do other things (test, exec, publish, etc.) will operate on the root project, unless one or more workspaces are specified in the workspace config.

node_modulesツリーを操作するコマンド(install、updateなど)は、ワークスペースをnode_modulesフォルダにリンクします。-他のことを行うコマンド(test、exec、publishなど)は、ワークスペース設定で1つ以上のワークスペースが指定されていない限り、ルートプロジェクトを操作します。

 

This value is not exported to the environment for child processes.

この値は子プロセスの環境にはエクスポートされません。

 

workspaces-update

    ・Default: true
    ・Type: Boolean

 

If set to true, the npm cli will run an update after operations that may possibly change the workspaces installed to the node_modules folder.

trueに設定すると、npm cliは、node_modulesフォルダーにインストールされたワークスペースを変更する可能性のある操作の後に更新を実行します。

 

include-workspace-root

    ・Default: false
    ・Type: Boolean

 

Include the workspace root when workspaces are enabled for a command.

ワークスペースがコマンドに対して有効になっている場合は、ワークスペースルートを含めます。

 

When false, specifying individual workspaces via the workspace config, or all workspaces via the workspaces flag, will cause npm to operate only on the specified workspaces, and not on the root project.

falseの場合、workspace configを使用して個々のワークスペースを指定するか、workspacesフラグを使用してすべてのワークスペースを指定すると、npmは指定されたワークスペースでのみ動作し、ルートプロジェクトでは動作しません。

 

This value is not exported to the environment for child processes.

この値は子プロセスの環境にはエクスポートされません。

 

See Also

    package spec
    init-package-json module
    package.json
    npm version
    npm scope
    npm exec
    npm workspaces

 

次回は、

npm-install

からです。