Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deploy node_modules folder to production server? [closed]

I making a very simple website (like pastebin) in PHP (LAMP server). This site uses jquery and bootstrap. I was using bower earlier, but I want to switch to using npm instead.

My directory structure is like this:

  • public
    • html
      • index.php
      • node_modules
        • jquery
          • dist
            • jquery.js
          • plus dozens of needless folders
        • bootstrap
          • ...

When I was using bower, I used to upload the whole bower_components folder to my FTP server. My node_modules folder is pretty large and contains hundreds of files and so I'm not sure if I should be uploading it?

I guess I can just upload package.json and run npm install on the command line of my webserver after uploading, but will exposing node_modules folder to the public pose potential security risks?

like image 874
supersan Avatar asked Aug 14 '17 14:08

supersan


People also ask

Do I need to push node_modules in production?

No, You don't need to push your node_modules folder to production whether it is a static export or dynamic build. When you export a static build the source file is converted into HTML & js files. So there is no need for node modules on production env.

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)

Do I need node_modules in production Nextjs?

No, you don't need to add your node_modules . Would recommend you check out the docs about how to go about deploying your Next. js application, but in essence: run next build.


1 Answers

Your node_modules folder should be in your .gitignore -if you are using git-. And no, you don't upload the whole folder. You, as you say, run npm install. This also applies to bower, you shouldn't upload the bower_components folder, you should run the command in your server.

As for the other part of the question, you shouldn't either expose node_modules. What you do is use some task manager or some bundler (gulp, grunt, webpack) and create a bundled (or concatted) version of your files (css and js). Once you have this file, that's what you expose. That's what you include in your index, for example <script src="dist/bundle.min.js"></script>

like image 86
yBrodsky Avatar answered Oct 28 '22 10:10

yBrodsky