Disallow require() expressions which import private modules (n/no-unpublished-require)
💼 This rule is enabled in the following configs: ☑️ flat/recommended, 🟢 flat/recommended-module, ✅ flat/recommended-script, ☑️ recommended, 🟢 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.
📖 Rule Details
If a source code file satisfies all of the following conditions, the file is *published*.
"files"field ofpackage.jsonincludes the file or"files"field ofpackage.jsondoes not exist..npmignoredoes 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.npmignoreor"files"field ofpackage.json.
Options
{
"rules": {
"n/no-unpublished-require": ["error", {
"allowModules": [],
"convertPath": null,
"tryExtensions": [".js", ".json", ".node"]
}]
}
}allowModules
This can be configured in the rule options or as a shared setting settings.allowModules. Please see the shared settings documentation for more information.
resolvePaths
This can be configured in the rule options or as a shared setting settings.resolvePaths. Please see the shared settings documentation for more information.
convertPath
This can be configured in the rule options or as a shared setting settings.convertPath. Please see the shared settings documentation for more information.
tryExtensions
This can be configured in the rule options or as a shared setting settings.tryExtensions. Please see the shared settings documentation for more information.