Heroku missing module - node.js

I'm trying to deploy a simple node.js server to Heroku, but keep running into this error:
Error: Cannot find module 'morgan'
I'm using morgan to do some HTTP logging, i have morgan in my package.json under the devDependencies (along with many others)
"devDependencies": {
"morgan": "~1.5.0"
}
I can do npm install locally and start the server without any issues, but when I deploy to Heroku I keep running into that error once it starts the server.
Is there anything I'm forgetting?

By default Heroku execute npm install --production which avoid install devDependencies, quoting literally documentation of Heroku.
The Heroku node buildpack runs npm install --production, which doesn’t install devDependencies in your package.json file. If you wish to install development dependencies when deploying to Heroku, you should move your build dependencies (such as grunt plugins) from devDependencies to dependencies in package.json.
If you use the Morgan module, you need move it to 'dependencies'.
Source: https://devcenter.heroku.com/articles/nodejs-support

Related

google-auth-library has missing dependencies when installed

I am trying to use google-auth-library with a react app but am getting various errors which seem to indicate missing dependencies. The errors are all in a format similar to this:
Module not found: Error: Can't resolve 'buffer' in '/private/tmp/minimal-example/node_modules/buffer-equal-constant-time'
The error messages include details on how to add a polyfill for the missing dependencies, but even after going through and adding all these polyfills I am unable to use the library. The logs for the development server still show errors for missing files, and the browser console has the following error:
GET http://metadata.google.internal./computeMetadata/v1/instance net::ERR_NAME_NOT_RESOLVED
Steps to reproduce:
Install Node 16.13.2
Create a react app npx create-react-app minimal-example
Enter directory for the created project cd minimal-example
Install google-auth-library npm install google-auth-library --save
Add the following code to ./src/App.js
const {GoogleAuth} = require('google-auth-library');
Start the development server npm start
Observe errors
What am I doing wrong?
I Have the same problem with same google-auth-library package!
This is a problem with using webpack 5 on React scripts=> v5
Have three solutions to resolve:
downgrade react-scripts to version 4.0.3. on package.json file.
Overwrite the config webpack config, but, i will need install react-app-rewired to do this, because react app not have webpack.config on tree app files.
you can eject the script to root folder executing npm run eject, but caution, this action is permanent, more here: https://stackoverflow.com/a/48395890
Some people answered this problem in How to Polyfill node core modules in webpack 5
and https://github.com/facebook/create-react-app/issues/12072

Whenever I try to run my server via Heroku I get "Error: Cannot find module 'dotenv'"

Browsing stack overflow, I noticed that a common fix is make sure that dotenv is in dependencies and not just devDependencies. Here's a screenshot of the log and the package.json. You'll notice that dotenv is indeed in my dependencies. I also deleted and reinstalled node_modules multiple times but still no luck.
I am requiring the dotenv at the top of my server file as shown here
I fixed the issue. I decided to try uninstalling dotenv and reinstalling, this worked. Apparently, this was different from deleting node modules and npm install.
I see dotenv is added to a devDependency: devDependency are special and are only installed when you do a npm install on the package.json which contains them. Usually, you'll add your development tooling (like test runners, etc.) in devDependencies since they are not required for the application to run.
When we run an application in production, we install dependencies using npm install --production which only installs the dependencies mentioned in the dependencies section of package.json. This is also what Heroku does for you (check their buildpack for Node.js!)
To install a devDependency, you would do: npm i -D <package name>; and to install a production dependency (like your web framework like express), you'd do npm i -S express.
Try running npm i -D dotenv and check if it works (it may also work if you just do a npm i dotenv.

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

react-scripts required old version of webpack

I installed webpack into my react project, but the same error occurred every time when I use npm start:
The react-scripts package provided by Create React App requires a dependency:
"webpack": "4.42.0"
Don't try to install it manually: your package manager does it automatically.
However, a different version of webpack was detected higher up in the tree:
C:\react\minimoj\node_modules\webpack (version: 4.43.0)
I tried to remove package-loc.json file + all node_modules + remove webpack name from package.json and reinstall it with npm install. Also I tried same with yarn, but it didn't help. I see that in node_modules version is 4.43 and after I use npm install webpack in the file package-loc.json it shows 4.43, but at the moment when I use npm start it changed to 4.42 and error occur. In addition I also reinstall node.js but it didn't help.
create .env file and add SKIP_PREFLIGHT_CHECK=true
I had similar with a newer webpack version being installed than what react_scripts required.
To fix I deleted webpack and webpack-dev-server from my project's node_modules, went to a console outside of the project dir and ran
npm uninstall webpack
npm uninstall webpack-dev-server
Then ran
npm install
npm run build
And all was well, as my package.json was correct without change.
Since webpack is mostly focused on common js according to some websites, I just tried rollup for my react project and it proved to be much more effective as it supports es+. I would highly recommend it. I took a couple minutes to have everything setup and ready. Also, as a good practice it is better to use yarn as npm has had a lot of known issues for node app management.

How to use "underscore" module in a nodejs application on Heroku?

I have a Node.js application deployed on Heroku.
I need to use the "underscore" module in the "\cloud\main.js".
I have tried the following in the main.js but it was unsuccessful:
var _ = require('underscore');
When pushing the changes to git for deployment to Heroku, it failed and gave "Cannot find the module underscore".
What is the correct way to use the underscore module in Node.js on Heroku?
It looks like module 'underscore' is not on heroku.
Update your package.json file ,Include 'underscore' in it.
Open your package.json, add following dependency :
"dependencies": {
"underscore": "*" // Specify required version
}
Now push modified package.json back to heroku.
On app startup heroku will automatically fetch the modules specified in package.json
Do you have a package.json? To deploy a nodejs application to heroku, you need to include your dependencies in package.json:
https://devcenter.heroku.com/articles/deploying-nodejs#declare-app-dependencies
If you don't have a package.json, you can generate one in your current working directory by running:
npm init
After that, install the underscore module with the --save option:
npm install underscore --save
Hope this helps!

Resources