Cannot find module 'webpack-dev-middleware' - node.js

I'm making a nodejs + expressjs server, and a react fat client (SPA). After I install this package.json:
{
"name": "express-react",
"version": "1.0.0",
"description": "Some basic ReactJS",
"author": "Greg",
"license": "ISC",
"private": true,
"keywords": [
"reactjs",
"webpack"
],
"scripts": {
"start": "node ./bin/www"
},
"dependencies": {
"body-parser": "~1.18.2",
"cookie-parser": "~1.4.3",
"debug": "~2.6.9",
"express": "~4.15.5",
"morgan": "~1.9.0",
"pug": "^2.0.0-rc.4",
"react": "^16.2.0",
"react-dom": "^16.2.0",
"react-redux": "^5.0.6",
"redux": "^3.7.2",
"redux-logger": "^3.0.6",
"serve-favicon": "~2.4.5"
},
"devDependencies": {
"babel-core": "^6.26.0",
"babel-loader": "^7.1.2",
"babel-plugin-react-html-attrs": "^2.1.0",
"babel-plugin-transform-class-properties": "^6.24.1",
"babel-plugin-transform-decorators-legacy": "^1.3.4",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"babel-preset-stage-0": "^6.24.1",
"webpack": "^3.10.0",
"webpack-dev-middleware": "^2.0.5"
}
}
I got this error when I run DEBUG=express-react:* & npm start command:
Error: Cannot find module 'webpack-dev-middleware'
My app.js was generated by the express-generator:
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var config = require('./webpack.config.js');
const webpackmiddleware = require('webpack-dev-middleware');
var webpack = require('webpack');
const compiler = webpack(config);
var index = require('./routes/index');
var users = require('./routes/users');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
app.use(webpackmiddleware(compiler, {
noInfo: true,
publicPath: config.output.publicPath,
stats: {colors: true},
}));
.....
It confuses me.. I defined the webpack-dev-middleware package in the package.json file, but it said its not found.. why?
Have anybody met this problem before?
Thanks for the responses / advices in advance!

webpack-dev-middleware is mentioned in your devDependencies, not dependencies.
If your NODE_ENV is set to production, npm install only installs production dependencies and excludes development dependencies:
To install devDependencies, run:
npm install --dev

Related

Heroku h12 and 503 errors are occured

enter image description hereI want to deploy the node.js server to heroku. But when I'm trying to get JSON file from server, h12, 503 errors were occured...
This is my git address. Branch is CHOI-HYO-GIL.
https://github.com/js-yu1592/capstone-design.git
This is package.json
{
enter image description here
"name": "lastproject",
"version": "1.0.0",
"description": "",
"main": "index1.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon index1.js"
},
"engines": {
"npm": "6.13.6",
"node": "13.5.0"
}
,
"author": "",
"license": "ISC",
"dependencies": {
"bcrypt": "^3.0.7",
"bcrypt-nodejs": "0.0.3",
"cookie-parser": "^1.4.4",
"dotenv": "^8.2.0",
"ejs": "^3.0.1",
"express": "^4.17.1",
"express-session": "^1.17.0",
"firebase": "^7.8.0",
"firebase-admin": "^8.9.2",
"firebaseui": "^4.4.0",
"morgan": "^1.9.1",
"mysql2": "^2.1.0",
"passport": "^0.4.1",
"passport-local": "^1.0.0",
"pug": "^2.0.4",
"sequelize": "^5.21.3",
"sequelize-cli": "^5.5.1",
"serve-favicon": "^2.5.0",
"session-file-store": "^1.4.0",
"socket.io": "^2.3.0"
},
"devDependencies": {
"connect-flash": "^0.1.1",
"nodemon": "^2.0.2"
}
}
This is index1.js
const express = require('express')
const port =process.env.PORT||3000;
const app=express()
const bodyParser = require('body-parser');
require('dotenv').config();
const { sequelize } = require('./models');
var admin = require("firebase-admin");
var serviceAccount = require("./graduation-f5a8d-firebase-adminsdk-3a1to-44789ec1c4.json");
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://graduation-f5a8d.firebaseio.com"
});
var user_info=require('./routes/user_info')
var user_fish=require('./routes/user_fish')
var home=require('./routes/home')
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json())
app.use(express.static('public'));
app.use('/user_fish',user_fish);
app.use('/user_info',user_info);
app.use('/',home)
app.listen(port, ()=>console.log('Example app listening on port ${port}!'))

I get 404 error in the localhost even though I already did "npm run build"

I'm trying to send a small demo made with React to production. I did not make the demo with create-react-app, I did it by hand. I used express.js to serve the application.
When I run the command "SET NODE_ENV = production node server.js" (I'm in Windows, that's why SET) and I go to the localhost I keep getting GET / 404, even though I already made the command "npm run build".
Since it's the first time I do this, I really have no clue what happens.
This is my package.json:
{
"name": "app",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"build": "webpack --colors",
"serve": "webpack-dev-server --content-base build/ --color --
progress",
"start": "npm run build && npm run serve",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Pepe",
"license": "MIT",
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-core": "^6.26.0",
"babel-eslint": "^8.2.2",
"babel-loader": "^7.1.2",
"babel-preset-env": "^1.4.0",
"babel-preset-react": "^6.24.1",
"babel-preset-stage-2": "^6.24.1",
"css-loader": "^0.28.9",
"eslint": "3.15.0",
"eslint-config-airbnb": "14.1.0",
"eslint-loader": "^1.9.0",
"eslint-plugin-import": "2.2.0",
"eslint-plugin-jsx-a11y": "4.0.0",
"eslint-plugin-react": "6.10.0",
"extract-text-webpack-plugin": "^3.0.2",
"html-webpack-plugin": "^2.30.1",
"identity-obj-proxy": "^3.0.0",
"jshint": "^2.9.6",
"style-loader": "^0.20.2",
"url-loader": "^0.5.8",
"webpack": "^3.3.0",
"webpack-dev-server": "^2.9.7"
},
"dependencies": {
"bootstrap": "^4.0.0",
"compression": "^1.7.3",
"express": "^4.16.4",
"mime": "^1.4.1",
"morgan": "^1.9.1",
"normalize.css": "^8.0.0",
"react": "^16.2.0",
"react-dom": "^16.2.0",
"react-router-dom": "^4.1.1",
"uuid": "^3.2.1"
},
"standard": {
"ignore": [
"/build/"
]
},
"eslintConfig": {
"extends": [
"standard",
"standard-jsx"
]
},
"stylelint": {
"extends": "stylelint-config-standard"
},
"optionalDependencies": {
"win-node-env": "^0.4.0"
}
}
And here is my server.js:
const { createServer } = require('http');
const express = require('express');
const compression = require('compression');
const morgan = require('morgan');
const path = require('path');
const normalizePortprocess = port => parseInt(port, 10);
const PORT = normalizePortprocess(process.env.PORT || 3000);
const app = express();
const dev = app.get('env') !== 'production';
if (!dev) {
app.disable('x-powered-by');
app.use(compression());
app.use(morgan('common'));
app.use(express.static(path.resolve(__dirname, 'build')));
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'build', 'index.html'));
});
}
if (dev) {
app.use(morgan('dev'));
}
const server = createServer(app);
server.listen(PORT, err => {
if(err) throw new err;
console.log(`Server listen in ${PORT}`);
});
Everything would seem to work correctly except that despite having executed to many times the command "npm run build", still nothing appears
You are running in a development environment, and only handling that GET request if !dev.
if (!dev) { // here is the problem
// ...
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'build', 'index.html'));
});
}
Instead, either remove the if statement and always handle this route, or change it to if(dev).

Nodejs app on heroku some files dont load status H10

I have created my startup's website! It took me 5 months. This is my fist Node.js web application. I finished 2 days ago and uploaded my project on Heroku. Happy beginning!
But I have a big issue with the error that comes back every 3 or 4 page refreshes, the consol gives me status 503 "name.css or name.js" service unavailable and in my Heroku logs I see H10 error the same "Service unavailable".
This happens with different files js, css, sometime images can't load, never the same files.
I'am on MacBook pro and uses atom.
Can anybody tell me what's going on?
Here is my app.js
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var exphbs = require('express-handlebars');
var expressValidator = require('express-validator');
var session = require('express-session');
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
var mongo = require('mongodb');
var mongoose = require('mongoose');
var mongorelation = require('mongo-relation');
var formidable = require('formidable');
var fs = require('fs');
var device = require('express-device');
var app = express();
mongoose.connect("**************");
var db = mongoose.connection;
var routes = require('./routes/index');
var users = require('./routes/users');
app.set('views', path.join(__dirname, 'views'));
app.engine('.hbs', exphbs({
defaultLayout:'layout',
partialsDir: __dirname + '/views/utils/',
extname: '.hbs',
helpers: {
last: function(array){return array[array.length -1].msg;},
subject: function(str){if (str.length > 50) return str.substring(0,50) + '...'; return str; }
}
})
);
app.set('view engine', '.hbs');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(device.capture());
app.use(express.static(path.join(__dirname, '/public')));
app.use(session({
secret: 'secret',
saveUninitialized: true,
resave: true
}));
app.use(passport.initialize());
app.use(passport.session());
app.use(expressValidator({
errorFormatter: function(param, msg, value) {
var namespace = param.split('.')
, root = namespace.shift()
, formParam = root;
while(namespace.length) {
formParam += '[' + namespace.shift() + ']';
}
return {
param : formParam,
msg : msg,
value : value
};
}
}));
app.use('/', routes);
app.use('/users', users);
app.set('port', (process.env.PORT || 3000));
app.listen(app.get('port'), function(){
console.log('Server started on port '+app.get('port'));
});
And here is my package.json
{
"name": "compname",
"version": "1.0.0",
"description": "compdesc",
"private": false,
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "authors",
"license": "ISC",
"dependencies": {
"bcryptjs": "*",
"body-parser": "*",
"connect-timeout": "^1.8.0",
"cookie-parser": "*",
"dotenv": "^4.0.0",
"express": "*",
"express-device": "^0.4.2",
"express-handlebars": "*",
"express-handlebars-paginate": "^1.0.3",
"express-messages": "*",
"express-session": "*",
"express-validator": "*",
"formidable": "^1.0.17",
"handlebars-helper-eachitems": "^0.1.2",
"handlebars-helper-paginate": "^0.2.0",
"handlebars-paginate": "^0.1.0",
"http": "0.0.0",
"https": "^1.0.0",
"image-size": "^0.5.0",
"imagesloaded": "^4.1.1",
"jquery": "^3.1.1",
"masonry-layout": "^4.1.1",
"moment": "^2.17.1",
"mongo-relation": "^0.5.4",
"mongodb": "*",
"mongoose": "*",
"mongoose-aggregate-paginate": "^1.0.5",
"mongoose-relationship": "^0.1.5",
"multer": "^1.2.0",
"multer-storage-s3": "^1.1.1",
"node-uuid": "^1.4.7",
"nodemailer": "~0.7.1",
"passport": "*",
"passport-http": "*",
"passport-local": "*",
"promise": "^7.1.1",
"pusher": "^1.5.1",
"random-js": "^1.0.8",
"s3fs": "^2.5.0",
"socket.io": "^1.7.2",
"twilio": "^2.11.1"
},
"devDependencies": {
"handlebars-helper-paginate": "^0.2.0"
}
}
After a long day of suffering, I finally got it to work. I updated my versions of node 6.9.1 to 7.8.0 and npm from 3.0... to 4.4.4 and i spécified it in the package.json.
{
"name": "Fason",
"version": "1.0.0",
"description": "Company Name",
"private": false,
"main": "app.js",
"author": "Me",
"license": "ISC",
"dependencies": {
"passport-local": "*",
"promise": "^7.1.1",
"pusher": "^1.5.1",
"random-js": "^1.0.8",
"s3fs": "^2.5.0",
"socket.io": "^1.7.2",
"twilio": "^2.11.1"
},
"engines": {
"node": "7.8.0",
"npm": "4.4.4"
}
}
like that.
Hope that this will help someone.

Getting an error when deploying to heroku "faliled to load resource 404 " bundle.js

I am trying to deploy my app to heroku but it's throwing this error that I listed in the title. I'm using webpack to bundle the files into the file bundle.js I'm just not sure how to deploy this to heroku so that bundle.js will be available. How do you reconcile webpack with heroku? I am going to attach my db.js file and package.json file.
db.js
var Sequelize = require('sequelize');
var env = process.env.NODE_ENV || 'development';
var sequelize;
if (env === 'production') {
sequelize = new Sequelize(process.env.DATABASE_URL, {
dialect: 'postgres'
}); // This if statement should execute if the app is run on Heroku
} else {
sequelize = new Sequelize(undefined, undefined, undefined, {
'dialect': 'sqlite',
'storage': __dirname + '/data/practice-todo-api.sqlite'
});
}
var db = {};
db.todo = sequelize.import(__dirname + '/models/todo.js');
db.user = sequelize.import(__dirname + '/models/user.js');
db.token = sequelize.import(__dirname + '/models/token.js');
db.sequelize = sequelize;
db.Sequelize = Sequelize;
db.todo.belongsTo(db.user);
db.user.hasMany(db.todo);
module.exports = db;
package.json
{
"name": "practice-todo-api",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "node server.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"axios": "^0.9.1",
"bcrypt": "^0.8.5",
"body-parser": "^1.13.3",
"crypto-js": "^3.1.5",
"express": "^4.13.3",
"jsonwebtoken": "^5.0.5",
"lodash": "^3.10.1",
"moment": "^2.11.2",
"pg": "^4.4.1",
"pg-hstore": "^2.3.2",
"react": "^0.14.7",
"react-dom": "^0.14.7",
"react-redux": "^4.0.0",
"react-router": "^2.0.0-rc5",
"redux": "^3.0.4",
"redux-form": "^4.1.3",
"redux-promise": "^0.5.1",
"request": "^2.60.0",
"sequelize": "^3.17.1",
"sqlite3": "^3.1.1",
"underscore": "^1.8.3"
},
"devDependencies": {
"babel-core": "^6.5.1",
"babel-loader": "^6.2.2",
"babel-preset-es2015": "^6.5.0",
"babel-preset-react": "^6.5.0",
"babel-preset-stage-0": "^6.5.0",
"file-loader": "^0.8.5",
"webpack": "^1.12.13"
}
}
Heroku recognizes a script called postinstall as part of the deployment lifecycle, which it executes after running npm install. In the postinstall script, specify the command you use to transpile your bundle.js file.
For example:
...
"scripts": {
"build": "webpack --config ./config/webpack.config.js --progress --colors",
"start": "node server.js",
"postinstall": "npm run build" // referencing the "build" script above
},
...
Some helpful resources:
http://ditrospecta.com/javascript/react/es6/webpack/heroku/2015/08/08/deploying-react-webpack-heroku.html
https://github.com/alanbsmith/react-node-example/
https://github.com/data-creative/express-on-rails-starter-app/tree/mongo-deploy-react

Cordova: Cannot find modulde body-parser

I have run this command on my root console
npm install --save
At the root of my folder
This is a snippet from my ionic-gcm.js
var app = express();
var bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
var server = app.listen(3000, function(){
var host = server.address().address
var port = server.address().port
console.log('Example app listening at http://%s:%s', host, port)
});
When I run this command, node ionic-gcm.js
I keep getting this error
Error: Cannot find module body-parser
===============================package.json======================
{
"name": "ionic-gcm",
"version": "1.0.0",
"description": "ionic-gcm: An Ionic project",
"dependencies": {
"express": "^4.13.3",
"gulp": "^3.5.6",
"gulp-concat": "^2.2.0",
"gulp-minify-css": "^0.3.0",
"gulp-rename": "^1.2.0",
"gulp-sass": "^1.3.3",
"node-gcm": "^0.11.0",
"server": "0.0.3"
},
"devDependencies": {
"bower": "^1.3.3",
"gulp-util": "^2.2.14",
"shelljs": "^0.3.0"
},
"cordovaPlugins": [
"org.apache.cordova.device",
"org.apache.cordova.console",
"com.ionic.keyboard",
"cordova-plugin-whitelist"
],
"cordovaPlatforms": [
"android"
]
}
you are missing body-parser. just type
npm install --save body-parser

Resources