💼 This rule is enabled in the following configs: 🟢 recommended-module
, ✅ recommended-script
.
If a require()
expression's target is not published, the program works in local, but will not work after published to npm.
This rule disallows require()
expressions of unpublished files/modules.
If a source code file satisfies all of the following conditions, the file is *published*.
"files"
field ofpackage.json
includes the file or"files"
field ofpackage.json
does not exist..npmignore
does not include the file.
Then this rule warns require()
expressions in *published* files if the require()
expression imports *unpublished* files or the packages of devDependencies
.
This intends to prevent "Module Not Found" error after
npm publish
.
💡 If you want to importdevDependencies
, please write.npmignore
or"files"
field ofpackage.json
.
{
"rules": {
"n/no-unpublished-require": ["error", {
"allowModules": [],
"convertPath": null,
"tryExtensions": [".js", ".json", ".node"]
}]
}
}
This can be configured in the rule options or as a shared setting settings.allowModules
.
Please see the shared settings documentation for more information.
This can be configured in the rule options or as a shared setting settings.resolvePaths
.
Please see the shared settings documentation for more information.
This can be configured in the rule options or as a shared setting settings.convertPath
.
Please see the shared settings documentation for more information.
This can be configured in the rule options or as a shared setting settings.tryExtensions
.
Please see the shared settings documentation for more information.
In a private package you sometimes want to disable checking for unpublished dependencies, e.g. if the package is not published.
However, there are situations where you want to mark it as private, though still ensure only published dependencies are used in your source code. An example, for such a case would be a package that is deployed to a server.
Defaults to true
.
package.json:
{
"private": true,
...
}
{
"rules": {
"n/no-unpublished-import": ["error", {
"ignorePrivate": true
}]
}
}