Is it relevant to use dev dependencies in a Node / Express backend? - node.js

I understand using package.json dev dependencies when you build your code. But if your application is an Express backend, does it make any difference using dev or regular dependencies in package.json?
An example of a backend dev dependency I would use is nodemon.
The only thing I can think of is that hosting providers might somehow install only non-dev dependencies when running, but I can't find details if that actually happens.

Related

How to deploy Node.js / Express app with typescript on heroku?

I want to deploy node.js / express server app on heroku.
I tried with this blog.
But it didn't work. The parts of my code are below.
server.ts
package.json
Ahh. You have messed up in the package.json file. What you have done is you have included the dependencies in the devDependencies.
Remember one thing always, when you are going to push an app to Heroku it prunes the devDependencies.
Actually, this is the main cause your server is not starting up because there are not required modules.
Run this command to move your devDependencies to dependencies:
npm install <module-name> --save-dev
This thing will solve your problem. If you face any other problem related to the configuration then follow this article - https://dev.to/hte305/simple-deploy-typescript-application-to-heroku-5b6g

Should I save or save-dev client-side webpacked dependencies?

I have dependencies that are only getting used client-side (and are getting packed into dist files on compilation through webpack). Should I save them as dev dependencies or just regular dependencies?
I'm just thinking in a server environment I'd have to recompile each time I update. Or would I? Does anyone have any pointers?
I would suggest using --save in your situation. --save is for those packages being used in production and --save-dev for those used when you are developing your app.
--save is used to save packages required for the application to run.
Here's an article for more details on what it means for a package to be a development dependency vs a dependency.

How to deploy one app from a large monorepo with dependencies to packages in the same repo to google app engine?

I have a large node.js monorepo with several applications and packages and inter dependencies. It is all managed with yarn workspaces and a little bit of lerna. Everything works great for me, however I am having trouble trying to deploy one of the applications in this monorepo to google app engine.
The main issue is that the app engine wants to install packages that are located only locally and are not on npm, and it throws an error.
I've scoured the google cloud documentations but did not manage to find anything that I could use to specify custom node packages or anything similar.
Is there a way to make such a deployment without publishing the local packages to npm?
The basic structure of the app I want to deploy looks like this:
-root
-packages
-packageA
-package.json
-apps
-deployable-app
-package.json <-contains dependency: "packageA": "0.0.1"
-app.yaml
Create a minimal docker image by coping to the image only the deployable-app and the packages from the same monorepo that the deployable-app depends on (in your case it's packageA).
When installing, yarn will do all the work to link between them.
Notes:
Deterministic installation - It is recommended to do so in monorepos to force deterministic install because nodejs packag emanagers (pnpm, yarn, npm) does not read the lock files of the dependencies of your app so when you run install, and packageA is located in public/private npm-registry, the package manager will install packageA dependencies as he wants. But you have a yarn.lock file in your monorepo that described what exectly should be installed and in which version.
Small docker image, better caching - Copy only the local packages from your monorepo that your deployable-app needs and create a script that removed all devDependencies from all the package.jsons in the monorepo (inside the dockerfile). they are not needed in production. make your image small.
I have the same problem with Firebase Cloud Functions, so I decided to publish my packages into a private registry and configure the Cloud Function environment with a .npmrc to use my private registry. I suppose that you can do the same with App Engine.
For private registry, I already tried two: GitHub Package Registry(now in Beta) and Verdaccio(which is a self-hosted option)

Building a NodeJS app on Heroku deploy

What is the recommended approach to using build tools like Gulp and Browserify to build your app on deploy with Heroku? Heroku doesn't give much info other than it's possible.
The main reason I ask is because if you want to build your app on deploy you have to place your build tools in dependencies rather than devDependencies. That seems wrong to me and popular build tools like Gulp and Grunt instruct users to include them as dev dependencies so it seems like a common practice. I'm curious as to whether this is a subtle hint from Heroku to not build on deploy.
If Heroku is worried about dev dependencies polluting or compromising a production deployment npm prune --production can serve to prune non-production dependencies after postinstall scripts execute.

cannot find module express, how to install it to make it globally available?

I want to experiment with some node.js stuff and I installed it yesterday following someone's instructions on the web which got it up and running, and I got the standard Hello World web page up on the screen.
I now went to move onto another example, but in order to not clutter my home directory, I created a directory off of it (~/node) and created the files I needed in there. Low and behold, when it came time to run the service, I got no joy stating the express module couldn't be found.
The instructions told me to install express using the -g flag, but that didn't help. I even ran it again without any luck.
Now I've found this:
Cannot find module `express` | socket.io [node.js]
and it appears I have to install it again under the current directory. I have done that and it works. Is it the case it has to be installed under each directory that I want services running from? It seems an unnecessary duplication.
edit:
Not knowing much about js I thought I would go digging and found
app.use(express['static'](__dirname ));
and have realised this is probably the cause of my problem. Further research has found this: http://nodejs.org/api/modules.html#modules_loading_from_node_modules_folders and if I install it once in a higher level directory, that should 'solve' my problem. I'm not going to bother about uninstalling the module, but for my next project I will try it and see how it goes.
I don't know why the original author suggested the -g flag when installing express, since it hasn't seemed to work for me.
NPM is a really nice tool, allowing you to install node.js modules locally and globally.
Local module installation
If you want to use a module for your project, you have to install it locally.
That's why npm creates a subdirectory called node_modules inside your project directory.
If you use the same module for two different projects, npm will download the module and install it twice. That's perfectly normal, it helps you manage different versions of the same dependency.
The best way to manage dependencies and install modules for a specific project is to fill the package.json with your dependencies and install them using
npm install
inside your project directory.
To access your modules in your code, use the require() function.
For example, with expressjs :
var express = require('express');
var app = express();
...
Global module installation
npm allows you to install modules globally as well. But remember that installing a module globally only provides more commands in your terminal, as with expressjs and express(1).
In order to install expressjs globally, run this in your terminal
npm install -g express
If you want to use a globally installed module in a specific project, you also have to install it locally (in your project directory, without -g).
I hope this answers clearly your question.
Express is capable of generating a simple app structure when installed globally. See this link and scroll to Using express(1) to generate an app section. It's a good way to get you started easily.
Take a look into package.json, package.json in nodejitsu
All npm packages contain a file, usually in the project root, called package.json - this file holds various metadata relevant to the project. This file is used to give information to npm that allows it to identify the project as well as handle the project's dependencies
a package.json example:
{
"name": "CRUD",
"description": "A simple CRUD",
"author": "Only for learn",
"dependencies": {
"express": "*",
},
}
so for install the dependencies go to level that package.json is, and run npm install this one will install all the dependencies you need for the project.
EDIT
a package.json interactive guide
I have found that when setting up node.js projects and dependencies, using Grunt [http://gruntjs.com/] has a lot of advantages. Although there are lots of different ways to setup a node and express project there is a lot to be said for using the Douglas Crockford approach and 'going with the grain'. In this case Grunt is the grain as it is becoming the de-facto standard for setting up a node project and there are existing templates for the most common types of node.js projects. You can find Grunt Express here [https://github.com/blai/grunt-express].
In this case Grunt would provide you with a project structure consistent with others, setup dependencies file for the node package manager and auto generate the express project for you. Packages are kept in a node_modules directory. If you are familiar with maven you might recognize the 'convention over configuration approach'.

Resources