removeNPMAbsolutePaths is a small utility to remove the fields that npm adds to the modules in node_modules
containing local absolute paths.
It has been noted that the package.json
of modules in the node_modules
folder contain some extra fields like _args
and where
which contain the absolute path of the module. According to NPM those fields are not even used.
The problem comes when you are planning to package your application using electron, NW.js or similar and distribute it. You might not want to distribute files containing absolute paths within your computer.
A feature request has been raised to NPM to fix this issue but they have made clear they don't plan to fix this.
- npm/npm#12110 (feature request)
- npm/npm#10393 (discussion about the topic)
removeNPMAbsolutePaths simply loop through all the files in the given folder, open the files called package.json
and remove all the fields starting with an underscore (_
).
You can install removeNPMAbsolutePaths globally and use it from the command line
$ npm install -g removeNPMAbsolutePaths
$ removeNPMAbsolutePaths '<PROJECT_FOLDER>'
or use it from whithin your code
const removeNPMAbsolutePaths = require('removeNPMAbsolutePaths');
try {
const results = await removeNPMAbsolutePaths('<PROJECT_FOLDER>');
results.forEach(result => {
// Print only information about files that couldn't be processed
if (!result.success) {
console.log(result.err.message);
}
});
} catch(err) {
console.log(err.message);
}
Using removeNPMAbsolutePaths
from within Javascript returns a promise containing information about all the folders and files processed and whether they where successfully processed and rewritten or not.
removeNPMAbsolutePaths can be configured using tags. Tags can be added to the command line commands:
$ removeNPMAbsolutePaths '<PROJECT_FOLDER>' --force --fields _where _args
or passed programmatically in an options object
removeNPMAbsolutePaths('<PROJECT_FOLDER>', { force: true, fields: ['_where', '_args']});
removeNPMAbsolutePaths only rewrite to disk the files that it modifies. Passing the --force
tag will rewritte all the files even if they haven't been modfied. This might be useful if you want all the package.json files to have always exactly the same styling for example for hashing.
removeNPMAbsolutePaths by default removes all fields starting with _
. Passing the --fields
tag followed by a list of field names you want removed will cause it to remove only those ones you list. This might be useful if only some of the fields in package.json are bothering you.
MIT