How to empty the node_modules folder in preparation for deployment

How do I get started clearing the node_modules folder when preparing my code for deployment.

I make an application using node -webkit and prefer to include the smallest number of files when merging the final version of the application, since the unpacking process takes some time.

I looked through npm dedupe and used npm install --production to get rid of duplicates and extract only production files, however I still remain with Readme , benchmarks , tests and build files that I don't need.

What I would like to get for each module in the node_modules folder is the node_modules file, if it exists, package.json and all I need to run the module, but nothing more.

Question: How to automatically clear the node_modules directory for committing the SCM header a little in the same direction, but it's about making sure that there is not what I am looking for.

Question: The pure NPMs were again about the same as mine, but not quite there.

This answer helps, as it is a more efficient version for dedupe for combining the final application.

Update
I tried the user module associated with here , but it did not seem to work correctly, even after some involvement.

With all that said, I have not yet found the correct answer.


Here is an example of what I'm looking for.

In my project, I currently have two dependencies: socket.io and socket.io-client .

Together they make up 15 MB with 550 files in 110 folders.

Manual cleaning of Readme , makefile , VC++ assembly files, such as .pdb and .obj and other unnecessary files, I was able to compress it to 2.74 MB using 265 folders 73 .
These are just two modules.

I would like to know if there is a way to do this automatically, preferably with npm .

+7
npm node-modules
source share
6 answers

It’s good to clean the node_modules for deployment for the webkit application, its complexity, because of the modules inside the node_modules directory, node_modules installed with or without test files with other misc files, and so, if the module owner declared a .npmignore file with dir / files such as tests or examples that will be excluded from the packaging process when the owner publishes his module, but he will exist in the repository (git) as usual.

Exclude test code in npm package?

the above remains in the hands of the owners of the modules, if he "forgets" to do one, then the package will contain almost everything.


Please note that since you are not using the development socket.io package or socket.io-client package, this does not mean that you need npm install socket.io --save-dev , a simple npm install socket.io -V will install the production the package, since it was downloaded by its owner,
A possible workaround would be to make the grunt task clear your entire node_modules way you would like.

a couple of rules will be

  • test or tests or *test*.js files
  • build (not sure if it may contain some executables, which are sometimes necessary)
  • history.md

Gruntjs


I hope I somehow helped, also take a look at Tilemill and how they deploy their application.
+2
source share

This module is trying to intelligently empty the node_modules folder:

modclean

Install:

 npm install modclean -g 

or

 npm install modclean --save-dev 

Using:

 modclean 

It uses a default set of templates to remove unnecessary bloat from modules throughout the entire dependency tree.

+5
source share

Modules installed with NPM should never contain development files (i.e. tests, tests, etc.). If they are included, you should contact the module developer and ask to add them to .npmignore .

Note. Development files in this case mean the files needed to develop a real module, not your application.

Previously suggested, but the deploy task in grunt is probably a good idea. Just make sure to test application after cleaning. grunt-contrib-clean great for cleaning.

See .npmignore from connect for some ideas on which files / directories should not be in the production package.

+1
source share

You might be interested in this small find , which I have built over time. Keep in mind that this is not a “one-time solution”! You need to carefully check it according to your requirements. It is designed for node.js environments and will definitely destroy browser environments. I run in a bash script as a postinstall script in npm.

DON'T MISS COPY 'N PASTE . You have been warned!

 find node_modules \( \( -name "dist" -or -name "ts" -or -name "logos" -or -name "min" -or -name "test*" -or -name "doc*" -or -name "tst" -or -name "example*" -or -name "build" -or -name "man" -or -name "benchmark*" \) -and -type d \) -or \ \( \( -iname "readme*" -or -iname "changelog*" -or -iname "notice*" -or -iname "test*.js" -or -iname "*.min.js" \) -and -type f \) -or \ \( -path "*moment-timezone/data/unpacked*" -and -type d \) 

Of course, I did not add the final line | xargs rm -rf | xargs rm -rf . You can safely execute the above command without deleting anything, and then add the channel to xargs with rm to make this happen.

What does the find command do? I will explain the pattern by pattern.

  • \( \( -name "dist" -or -name "ts" -or -name "logos" -or -name "min" -or -name "test*" -or -name "doc*" -or -name "tst" -or -name "example*" -or -name "build" -or -name "man" -or -name "benchmark*" \) -and -type d \) => look for directories matching the text in quotation marks. * is a wildcard.

  • \( \( -iname "readme*" -or -iname "changelog*" -or -iname "notice*" -or -iname "test*.js" -or -iname "*.min.js" \) -and -type f \) => search for files in any folder where the quoted text matches, regardless of the case. Especially the "*.min.js" template can be dangerous for some people.

  • \( -path "*moment-timezone/data/unpacked*" -and -type d \) => delete the unpacked data from the moment. It also saves a lot of space.

Feel free to improve it!

+1
source share

I studied this for deployment in an AWS elastic hose. When I run the eb deploy command, it magically found the files to download and did not receive any of the bower_components or node_modules . I wondered how.

It turns out that eb deploy calls git archive under the hood. git archive checks the specified branch and zip all the files.

Presumably you are not doing things like your node_modules or bower_components directories on git, so the git archive might be the solution to your problem. If you want to avoid things like test cases and README files, and so on, you still need to do some tagging in git. But you start with a much smaller list, and you explicitly exclude the bulk of the material you want to exclude.

0
source share

I think you are looking for npm prune

npm prune [<name> [<name ...]]

This command removes extraneous packets. If the package name is provided that only packages matching one of the following are removed.

Foreign packages are packages that are not listed in the parent list of package dependencies.

Documentation https://docs.npmjs.com/cli/prune

-one
source share

All Articles