Is there any way to either (a) disallow a node application from require
/import
ing globally-installed npm modules, or (b) at least output a warning when a globally-installed module is used?
Reason being: I've repeatedly wound up in situations where a developer (myself included) incorporates a module in a node application but fails to add it to package.json
because it happens to be globally installed on their machine, and therefore there's no error on the local machine; but upon deployment to a system that doesn't have that module installed globally, of course, it fails. Would be convenient to just ensure that all modules are in fact included in package.json
.
If you find the same deprecation messages, then you can try to see if there's an open issue in GitHub discussing the deprecation messages. While you can run the npm install command to get the required package version, it's not necessary as the module should still work.
If you would like to remove all the packages that you have installed, you can use the npm -g ls command to find them, and then npm -g rm to remove them.
ESLint Way
1) use husky module: npm install husky --save-dev
2) install eslint and it's deps (see package.json example below).
3) example package.json:
{
"name": "shopping-cart-estimator-test",
"version": "1.0.0",
"description": "",
"scripts": {
"start": "node ports/http.js",
"eslint-check": "./node_modules/eslint/bin/eslint.js .",
"eslint-fix": "./node_modules/eslint/bin/eslint.js . --fix",
"test": "./node_modules/.bin/mocha test --exit"
},
"devDependencies": {
"babel-eslint": "^10.1.0",
"husky": "^4.2.5",
"mocha": "^7.1.2",
"eslint": "^7.0.0",
"eslint-config-import": "^0.13.0",
"eslint-config-standard": "^14.1.1",
"eslint-plugin-import": "^2.20.2",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-promise": "^4.2.1",
"eslint-plugin-standard": "^4.0.1"
},
"dependencies": {
"lodash": "^4.17.15"
},
"husky": {
"hooks": {
"pre-commit": "npm run eslint-check && npm test",
"pre-push": "npm run eslint-check && npm test"
}
}
}
4) create .eslintrc.js
file with import/no-extraneous-dependencies rule (it requires eslint-plugin-import, make sure it's in package.json):
module.exports = {
"extends": "standard",
"parser": "babel-eslint",
"rules": {
"semi": ["error", "always"],
"no-unused-vars": 1,
"spaced-comment": ["warn"],
"no-trailing-spaces": ["warn"],
"comma-dangle": ["error", {
"arrays": "always",
"objects": "always",
"imports": "never",
"exports": "never",
"functions": "never"
}],
"space-before-function-paren": ["error", {
"anonymous": "always",
"named": "never",
"asyncArrow": "always"
}],
"import/no-extraneous-dependencies": ["error", {"packageDir": __dirname}],
},
"overrides": [{
"files": ["spec/tests/*.js", "spec/tests/**/*.js"],
"rules": {
"no-unused-expressions": 0,
"no-unused-vars": 1
}
}]
};
"Custom Script way"
1) use husky module: npm install husky --save-dev
2) add hook to pre-commit, pre-push in package.json:
"husky": {
"hooks": {
"pre-commit": "npm test && node scripts/check-deps.js",
"pre-push": "npm test && node scripts/check-deps.js"
}
}
3) install dependency-tree: npm i --save dependency-tree
4) write scripts/check-deps.js
script that will find external (non package.json) dependencies and if they exist will:
console.warn('Found external dependency');
process.exit(-1);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With