Can I delete node_modules folder after compiling all required dependencies? - node.js

I have a project on Laravel 5.4; I'm absolutely new to front-end package managers.
I used npm to install 4 packages in my project, so my node_modules folder has about 210 MB.
I used Laravel Mix to combine in groups required dependencies and files, then I run npm run dev.
My webpack.mix.js looks like :
let mix = require('laravel-mix');
mix
.copy('node_modules/jquery/dist/jquery.js', 'public/js')
.combine([
'node_modules/video.js/dist/video-js.min.css',
'node_modules/videojs-record/dist/css/videojs.record.css'],'public/css/recordDependencies.css')
.combine([
'node_modules/video.js/dist/video.min.js',
'node_modules/recordrtc/RecordRTC.js',
'node_modules/webrtc-adapter/out/adapter.js',
'node_modules/videojs-record/dist/videojs.record.js',
'node_modules/jquery/dist/jquery.js',
'node_modules/blueimp-file-upload/js/vendor/jquery.ui.widget.js',
'node_modules/blueimp-file-upload/js/jquery.iframe-transport.js',
'node_modules/blueimp-file-upload/js/jquery.fileupload.js'
], 'public/js/recordDependencies.js');
So, it generates me this files. Making a small test, I replaced the node_js directory out of the project, and everything still works.
So, the question is :
Can I delete this huge folder - node_modules, or it must live in
kernel of my project, at the deploying as well ?

In frontend applications node_modules are only required for compiling resources. You would not want to deploy node_modules because they contain unobfuscated code. Only deploy compiled files.

Related

How to treeshake node_modules when building a nodejs backend project?

Could I treeshake node_modules when i build a nodejs backend project?
The node_modules is so big and there are too many small files in it,How can i treeshake it like the frontend do?
Too many small files in node_modules is not used in my project but when i want to deploy offline I must carry them from here to there.
node_modules
-- ramdajs
-- lodashjs
-- ...
How can I do treeshaking with node_modules in an express project.

How share node_modules across two node projects with PNPM?

i have a project with two nodeJS projects inside, a backend project with inside a folder "client" for the frontend.
Now i would like to make one common node_moduels folder both.
I found that PNPM could do that, but for me the documentation is not so clear. How doest it work?
How can i run pnpm i on my root folder and then make my node_modules accessable for my "client" folder?
Thank you!
On Linux or Mac, i usually symlink the module (ln -s) into my other projects' libs. Then i import or require the module from there. It's located directly in your codebase, not in node_modules, but it works just fine.
Just don't update the common module without keeping in mind all the dependant projects.
For easy versioning, you may choose to publish your module to npm (even as private), and update it with each push for each of the dependants.

Using NodeJS, Bower and GulpJS in a project, what should I exclude from my Git repository?

I recently started working on a web project. I'm using:
NodeJS as a server
Bower - to get all the dependencies
GulpJS - for build and other tasks
Git - to save my work
For simplicity, let's say that I'm only writing HTML/CSS/JavaScript.
It doesn't seem to make sense to save ALL the project files in Git including external JavaScript libraries, since I only want to save the core files (the files that I created and modified myself).
On the other hand, if I want to hand over the project to another developer and I only hand him my own files without all the dependencies and libraries, how will he know which dependencies to get? How will he be able to build and run the project?
(I'm still new to bower, gulp and node)
So, what files do I need to save in my repository (the minimum number of files) to be able to build and work on the project?
According to what you said, your .gitignore file should look like this :
node_modules
bower_components
dist
.tmp
On the other hand, if I want to hand over the project to another developer and I only hand him my own files without all the dependencies and libraries, how will he know which dependencies to get?
You don't include your dependencies such as the node_modules and bower_components but the package.json and bower.json are tracking those dependencies so that when a new user makes a clone, he only has to npm install and bower install.
This is, if you took care to use the --save or --save-dev flags when you npm or bower install new packages.
There's a quite active repository on github, containing predefined .gitignore files for different languages, platforms and buildtools.
https://github.com/github/gitignore
Although there's no bower- or gulp-specific configuration there (yet), I usually find it quite useful when trying new things.

Node.js npm dependencies in subfolder

I have a project in which I use node-webkit. node-webkit allows npm packages to be used for developing desktop applications. I make use of grunt to build my application.
My folder structure looks like this at the moment:
project root
node_modules/ (1)
package.json (1)
App/
node_modules/ (2)
package.json (2)
bower.json
bower_components/
...
controllers/
filters/
...
app.js
The npm dependencies for the application itself are kept within the App folder, but the dev dependencies for building the project are not related to the application source code, so i keep them in node_modules (1) inside the root folder. I also know that in a package.json file one can express dependencies and dev dependencies, exactly for this reason. I would rather have one package.json file in the root expressing ALL dependencies, including dev dependencies, but i would rather have a separation of those dependencies on folder level.
Two questions arise:
Is this a good way to organize my npm dependencies? If yes, awesome? If no, which I expect:
What is a better way to organize my dependencies? Is it possible to specify that dev dependencies go into folder a, and 'regular' dependencies go into folder b? If so, how do I do this?
In case anyone is wondering, this is the project i am talking about:
https://github.com/michahell/pinbored-webkit
[updated folder structure to include app.js for clarity]
It is perfectly fine to keep more than one package.json file and multiple node_module directories for a project. If you consider the parts as separate components.
An example might be if, you have one directory containing a node server, another containing a react app, and a third containing some kind of deployment script written in javascript.
#Michael package.json file contains all the dependencies related to that project.There is no need for multiple package files and multiple node_modules folders..
But you need to check where is your App.js file!!
your App.js , package.json must be in same folder unless configured.

is good practice to include my modules in node_modules for make require search easy,if not why not?

is good practice to include my modules in node_modules for make require search easy,if not why not?
Explanation:
In node.js cms calipso( https://github.com/cliftonc/calipso)
their modules not inside node_modules: then the include the modules without auto option:
calipso = require(path.join(rootpath, 'lib/calipso'));
vs if it was inside node_modules:
calipso = require('calipso');
node_modules is typically ignored in the version control (GIT, etc) and most of the developers assume that this folder contains only packages listed in the package.json. I can imagine the approach on updating the modules just by removing this folder completely and executing npm install. Considering these I would rather say that keeping own modules in node_modules is not consistent with the node.js workflow.
Update: this is assuming that "my modules" is actually just a set of files. If your modules are "npm" modules, that can be restored by executing "npm install" then this is completely fine to keep them in node_modules.

Resources