The 'exports' field of a package.json contains an invalid target.
Production Risk
Low. This is a package configuration error that occurs at application startup, making it easy to detect before deployment.
This error indicates a problem within a dependency's `package.json` file. The `exports` field, which controls which files are accessible from a package, has an invalid value. For instance, a target path might not start with `./` or might try to reference a file outside the package directory.
- 1A package's `package.json` `exports` map contains a value that is not a relative path starting with `./`.
- 2A target path in the `exports` field attempts to traverse upwards out of the package directory.
- 3The `package.json` file is malformed, causing the `exports` value to be parsed incorrectly.
This error occurs when Node.js parses a `package.json` file to resolve a module import and finds that a target mapping in the `exports` field is invalid according to the rules.
// This error is caused by a malformed package.json in a dependency.
// For example, if 'some-package/package.json' contains:
// { "exports": { ".": "/absolute/path/to/file.js" } }
try {
require('some-package');
} catch (err) {
console.error(err.code);
}expected output
ERR_INVALID_PACKAGE_TARGET
Fix 1
Update the Dependency
WHEN The error is in a third-party package.
npm update some-package
Why this works
Check for a newer version of the package. The author has likely already fixed the invalid `package.json` file. If not, report the issue to them.
Fix 2
Correct Your Own package.json
WHEN The error is in your own project's `package.json` file.
// In your package.json:
{
"name": "my-package",
"exports": {
".": "./index.js", // Correct: must be a relative path
"./feature": "./feature.js"
}
}Why this works
Ensure all paths in the `exports` field values are valid relative paths that start with `./` and stay within the package directory.
// This error is caused by a malformed package.json in a dependency.
// For example, if 'some-package/package.json' contains:
// { "exports": { ".": "/absolute/path/to/file.js" } }
try {
require('some-package');
} catch (err) { // this triggers ERR_INVALID_PACKAGE_TARGETtry {
// operation that may throw ERR_INVALID_PACKAGE_TARGET
riskyOperation()
} catch (err) {
if (err.code === 'ERR_INVALID_PACKAGE_TARGET') {
console.error('ERR_INVALID_PACKAGE_TARGET:', err.message)
} else {
throw err
}
}// Validate inputs before calling the operation
function safe_err_invalid_package_target(...args) {
// validate args here
return performOperation(...args)
}✕
https://github.com/nodejs/node/blob/main/lib/internal/modules/esm/resolve.js
More information ↗Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev