Guest User

Untitled

a guest
Jul 22nd, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.53 KB | None | 0 0
  1. ### Porduction code tips
  2.  
  3. #### npm
  4.  
  5. Package.json file is really important when we're thinking about production code. There's two important things to highlight here. One is the engine keyword, this specifies the minimum version of node that we want to use in our application.
  6.  
  7. ```json
  8. "engines" {
  9. "node": "6.0.0"
  10. }
  11. ```
  12. If someone then tries to install a package, npm will check whether the node version that the package was written in is the same or higher than the one we have specified. If it's less then an error message will be printed in the console warning us that we are adding a package written in a lower node version. Node changes really quickly and its important to put some constraints on our code. A similar type error could occur if someone is trying to install our application and they are using bleeding edge node. They will get an error to say our application or package is written in a node verison that is less than the one they are using.
  13.  
  14. Another issue when installing npm packages is their version number. Again these packages change very quickly and sometimes even small bug changes can break your app. Its good practice to explicitly tell npm that you only want that version number installed, you can then manually test new releases of dependencies and update them after they have been thoroughly tested.
  15.  
  16. ```
  17. npm config set save-exact=true
  18. ```
  19. You can get around this by running the command above in the terminal, this tells npm to do two things. Firstly it says if you run `npm save package-name` in the terminal then by default save the package name in the package.json, without having to specify the `--save` flag, because this is almost always what we want to do. The second thing it tells npm to do is to save the exact version number of the package and not update even small patch fixes, because some developers are introducing breaking changes in minor bug updates these days.
  20.  
  21. Another thing to consider when moving from a development environment to production is the environment variables. For instance we can set the port that we are working on to `process.env.PORT || 3000` so that node changes the port for us in production. How can we ensure that all our env variables remain dynamic and adjust accordingly in out project?
  22.  
  23. One library that we can use to help us with this is `foreman` which can be installed globally from npm. If node foreman doesn't find an env variable it will automatically set one fro us. If you have env variables set in a `.env` file, then `foreman` will automatically take all these variables and stick them in your process.env.
Add Comment
Please, Sign In to add comment