【保存版】Node.jsで「Error [ERR_PACKAGE_PATH_NOT_EXPORTED]」が発生した時の対処法まとめ
エラー「Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './lib/tokenize' is not defined by "exports" in the package.json of a module in node_modules」の解説
- モジュール内のサブパス
./lib/tokenize
が、package.json
ファイルのexports
フィールドに定義されていない。 - エラーが発生したモジュールは、
node_modules
フォルダ内に存在する。
簡単に言うと、モジュールを正しくインポートするために必要な情報が、package.json
ファイルに設定されていないことが原因です。
解決策
このエラーを解決するには、以下の2つの方法があります。
package.json
ファイルを開き、exports
フィールドに、インポートしたいサブパスを定義する必要があります。
{
"name": "my-module",
"version": "1.0.0",
"main": "./index.js",
"exports": {
"./lib/tokenize": "./lib/tokenize.js"
}
}
上記例では、./lib/tokenize
サブパスが ./lib/tokenize.js
ファイルを指すように定義されています。
相対パスを使用してインポートする
package.json
ファイルを修正したくない場合は、相対パスを使用してモジュールをインポートすることができます。
const tokenize = require('../node_modules/my-module/lib/tokenize');
上記例では、tokenize
変数に my-module
モジュールの lib/tokenize
サブパスを直接インポートしています。
その他の注意点
- エラーメッセージに記載されているモジュール名は、実際にエラーが発生しているモジュールとは異なる場合があります。原因となっているモジュールを見つけるために、エラーメッセージをよく確認する必要があります。
node_modules
フォルダ内のモジュールのバージョンが古くなっている可能性もあります。npm update
コマンドを実行して、すべてのモジュールを最新バージョンに更新することをおすすめします。
Exporting a single module:
{
"name": "my-module",
"version": "1.0.0",
"main": "./index.js",
"exports": {
"./lib/tokenize": "./lib/tokenize.js"
}
}
In this example, the exports
field defines that the ./lib/tokenize
subpath should be resolved to the ./lib/tokenize.js
file. This means that when a user imports the my-module
package and tries to access the tokenize
module, they will be importing the code from the ./lib/tokenize.js
file.
{
"name": "my-module",
"version": "1.0.0",
"main": "./index.js",
"exports": {
"./lib/math": "./lib/math.js",
"./lib/string": "./lib/string.js"
}
}
This example is similar to the previous one, but it exports multiple modules. This means that users can import the my-module
package and access the math
and string
modules using the following syntax:
const math = require('my-module/lib/math');
const string = require('my-module/lib/string');
{
"name": "my-module",
"version": "1.0.0",
"main": "./index.js",
"exports": {
"./lib": "./lib"
}
}
This example exports an entire directory, which means that users can import any file within the ./lib
directory using the following syntax:
const math = require('my-module/lib/math');
const string = require('my-module/lib/string');
In this case, the math
and string
modules will be resolved to the ./lib/math.js
and ./lib/string.js
files, respectively.
Exporting with conditional rules (Node.js 16+):
{
"name": "my-module",
"version": "1.0.0",
"main": "./index.js",
"exports": {
".": {
"import": "./dist/esm/index.js",
"require": "./dist/cjs/index.js"
},
"./types": "./dist/types/index.d.ts"
}
}
This example demonstrates the use of conditional exports, which allows you to specify different export mappings for different environments or module formats. In this case, the .
entry defines the exports for both CommonJS (using require
) and ES Module (using import
) formats, while the ./types
entry specifically exports the TypeScript declaration file.
Exporting with subpath patterns:
{
"name": "my-module",
"version": "1.0.0",
"main": "./index.js",
"exports": {
"./*": {
"import": "./dist/esm/*.js",
"require": "./dist/cjs/*.js"
},
"./types": "./dist/types/index.d.ts"
}
}
This example utilizes subpath patterns to match multiple modules within a directory. The "./*"
entry defines exports for all files within the ./dist/esm
directory for ES Modules and all files within the ./dist/cjs
directory for CommonJS modules. This allows users to import modules using relative paths without having to explicitly specify the file extension.
These examples provide a basic understanding of how to use the exports
field in package.json
to control module exports and enhance the flexibility of your Node.js packages.
その他の解決策
キャッシュをクリアする
まれに、エラーはnpmのキャッシュが原因で発生することがあります。このような場合は、以下のコマンドを実行してキャッシュをクリアしてみてください。
npm cache clean --force
node_modules フォルダを削除して再インストールする
問題が解決しない場合は、node_modules
フォルダを削除して、以下のコマンドを実行してモジュールを再インストールしてみてください。
rm -rf node_modules
npm install
互換性の問題を調査する
エラーが発生しているモジュールと、プロジェクトで使用している他のモジュールのバージョン間で互換性の問題が発生している可能性があります。
- 使用しているモジュールの最新バージョンを確認してください。
- 互換性に関する既知の問題がないか、モジュールのドキュメントや公開されている問題追跡システムを確認してください。
問題のあるモジュールを報告する
上記の方法で問題が解決しない場合は、問題が発生しているモジュールの開発者に問題を報告することを検討してください。
- モジュールの問題追跡システムに新しいイシューを作成してください。
- 問題の詳細な説明と、再現手順を提供してください。
代替モジュールを使用する
問題が発生しているモジュールの代替となるモジュールが存在する可能性があります。
- 他のモジュールのドキュメントやレビューを確認して、代替候補を評価してください。
- 代替モジュールがプロジェクトの要件を満たしていることを確認してください。
これらの方法は、Error [ERR_PACKAGE_PATH_NOT_EXPORTED]
エラーの解決に役立つ可能性があります。問題が解決しない場合は、さらに調査したり、コミュニティフォーラムやQ&Aサイトで助けを求めたりすることが必要になる場合があります。
node.js npm node-modules