0

Should I have to upload node_modules in production when I used --save when installing some package from NPM? They say that the packages that I have installed using --save is required to run the application. So I'm wondering if my app will still work on production if I don't upload the node_modules.

BTW I'm talking about babel.

npm install --save-dev @babel/core @babel/cli @babel/preset-env
npm install --save @babel/polyfill

Notice that the second line is using --save.

Enlighten me please.

bunbun
  • 2,516
  • 3
  • 28
  • 51
boosted_duck
  • 420
  • 3
  • 14

2 Answers2

2

I suggest dependent as babel install like this --save-dev because you dont need them in prodaction.

when you install your project in prodaction send the flag npm install --only=prod and then the libraries you install with --save-dev flag Will not be installed by npm in the node_modules folder

0

You must have node_modules present on the server for the app to work, absolutely. However, the usual way to do this is to run npm install on the server as part of a deployment script/process, NOT to upload node_modules manually. The --save will update the package.json file which tells npm which packages it needs to download when you run install. You upload the package.json to prod, and run npm install there.

see sharper
  • 8,591
  • 5
  • 29
  • 51
  • Hello, thanks for answering my question. Meaning, all the packages that uses --save-dev will not be install when i run npm install on the server? – boosted_duck Dec 13 '18 at 02:10
  • Presumably, you don't want to use dev dependencies in prod. See this answer for a full explanation: https://stackoverflow.com/questions/22891211/what-is-the-difference-between-save-and-save-dev – see sharper Dec 13 '18 at 02:13
  • So, i have to upload package.json without devDependencies on production so that NPM will only install the necessary packages to run my application? since I'm only going to use packages under devDependencies while building on my local machine? – boosted_duck Dec 13 '18 at 02:20
  • When running in production, you should set the environment variable NODE_ENV to 'production'. That way, npm will not install the dev dependencies in the packages.json file, only the main dependencies. – see sharper Dec 13 '18 at 02:27
  • NODE_ENV sounds new to me. is that something included when i install node.js? And i should also install node.js on server, right? – boosted_duck Dec 13 '18 at 02:59
  • NODE_ENV is just an environment variable. That is to say, you set it in the environment using `export NODE_ENV=production` before running `npm install`. And yes, you will need both node.js and npm installed on your production server. – see sharper Dec 13 '18 at 03:15
  • Have I answered your question? If so, marking as answer is always appreciated. Thanks. – see sharper Dec 13 '18 at 23:00
  • Hello, sorry for late reply. I also wanna ask when you say environment variable do you mean that it's already in my computer/machine without installing any application? – boosted_duck Dec 14 '18 at 00:47
  • Yeah, that's an important thing to know. An environment variable is a variable that you can set inside a bash shell using the `export` command. You can see the current environment vars using the `env` command. The variables are only set in your current bash session, so need to be set whenever you open a terminal shell, if you need them. – see sharper Dec 14 '18 at 04:14
  • Note you can also use the method suggested by Meni Roytenburd, i.e. install with `--only=prod`. See https://stackoverflow.com/questions/9268259/how-do-you-prevent-install-of-devdependencies-npm-modules-for-node-js-package – see sharper Dec 14 '18 at 04:16