Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to have a separate node_modules folder for devDependencies?

I've got a Node app that I'm deploying to Heroku. Their docs say it's best practice to check in your node_modules directory (i.e. don't gitignore it), for faster deploys and for dev/prod parity.

In my package.json, I've got loads of devDependencies (mostly Grunt plugins and all their deps) and a few regular production dependencies like Express. Heroku only needs the production deps. I'd rather not check in all my dev deps, because they come to about 50MB.

Is there some way to have a separate folder for you dev deps, e.g. node_modules_dev? If this was possible, then I could just add node_modules_dev to my .gitignore, and check in the regular production node_modules directory as per Heroku's advice.

Is there any way to do this? Or can you think of another way to do what I'm trying to do?

like image 251
callum Avatar asked Jul 29 '13 16:07

callum


People also ask

Is it mandatory to commit the node_modules folder in your application?

On the other hand, folder node_modules should not be committed to Git. Apart from their big size, commits including them can become distracting. The best solutions would be this: npm install should run in a CI environment that is similar to the production environment.

Does npm install create a node_modules folder?

npm install downloads a package and it's dependencies. npm install can be run with or without arguments. When run without arguments, npm install downloads dependencies defined in a package. json file and generates a node_modules folder with the installed modules.

Can I copy node_modules folder to another project?

Yes you can copy whole node_modules (have done it multiple times) from one project to another and use same package. json and package-lock (Will only save time in dependencies installation/download)

What is node_modules .bin folder?

The directory node_modules/.bin is where the binaries of the modules used by your project are stored, normally using symbolic links to the respective binaries in the corresponding module's directory.


2 Answers

I use a CI server to build, test, and deploy my files — so I was looking for a similar solution that would prevent me from needing to deploy extra dependencies and/or re-build on Heroku.

After all my tests run, I run npm prune --production, which removes devDependencies from node_modules, and then I push the result to Heroku.

No extra files go to the server, and deployment times are much faster since Heroku avoids having to build all the binaries usually found in Gulp/Grunt plugins.

like image 153
Jon Avatar answered Nov 15 '22 20:11

Jon


If you don't mind checking them in anyways, and your only concern is the resulting slug size (i.e.: not your git repo size, or transfer of that repo to Heroku), then simply add the relevant node_modules to .slugignore.

Docs: Ignoring files with .slugignore.

like image 28
Nitzan Shaked Avatar answered Nov 15 '22 21:11

Nitzan Shaked