disallow
require()expressions which import private modules
- ⭐️ This rule is included in
plugin:node-roikoren/recommendedpreset.
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.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.
{
"rules": {
"node-roikoren/no-unpublished-require": ["error", {
"allowModules": [],
"convertPath": [],
"tryExtensions": [".js", ".json", ".node"]
}]
}
}Some platforms have additional embedded modules.
For example, Electron has electron module.
We can specify additional embedded modules with this option. This option is an array of strings as module names.
{
"rules": {
"node-roikoren/no-unpublished-require": ["error", {
"allowModules": ["electron"]
}]
}
}If we use transpilers (e.g. Babel), perhaps the file path to a source code is never published.
convertPath option tells to the rule, it needs to convert file paths.
For example:
{
"rules": {
"node-roikoren/no-unpublished-require": ["error", {
"convertPath": [
{
"include": ["src/**/*.js"],
"exclude": ["**/*.spec.js"],
"replace": ["^src/(.+)$", "lib/$1"]
}
]
}]
}
}In this style, this option has the following shape as the same expression as above: {include: [<targetFiles>], replace: [<fromRegExp>, <toString>]}.
In addition, we can specify glob patterns to exclude files.
When an import path does not exist, this rule checks whether or not any of path.js, path.json, and path.node exists.
tryExtensions option is the extension list this rule uses at the time.
Default is [".js", ".json", ".node"].
The following options can be set by shared settings. Several rules have the same option, but we can set this option at once.
allowModulesconvertPathtryExtensions
For Example:
{
"settings": {
"node": {
"allowModules": ["electron"],
"convertPath": [{
"include": ["src/**/*.jsx"],
"replace": ["^src/(.+?)\\.jsx$", "lib/$1.js"]
}],
"tryExtensions": [".js", ".jsx", ".json"]
}
},
"rules": {
"node-roikoren/no-unpublished-require": "error"
}
}