Managing Packages with Npm - getting error "Cannot find module 'express'" when using glitch

Managing Packages with Npm - getting error "Cannot find module 'express'" when using glitch
0

#1

getting a log error from glitch regarding the express module which points to the server.js file:

Package.json file looks like this:

{
“author”: “Frederik Kars”,
“description”: “A project that does something awesome”,
“keywords”: [
“awesome”,
“freecodecamp”
],
“version”: “1.0”,
“license”: “MIT”,
“name”: “fcc-learn-npm-package-json”,
“dependencies”: {
“express”: “^4.16.3”,
“moment”: “^2.22.2”
},
“main”: “server.js”,
“scripts”: {
“start”: “node server.js”
},
“engines”: {
“node”: “8.11.2”
},
“repository”: {
“type”: “git”,
“url”: “https://idontknow/todo.git
}
}

cant figure out why i get the error. Have not change anything in the server.js file.

complete error code:
Error: Cannot find module ‘express’

at Function.Module._resolveFilename (module.js:547:15)

at Function.Module._load (module.js:474:25)

at Module.require (module.js:596:17)

at require (internal/module.js:11:18)

at Object. (/app/server.js:9:15)

at Module._compile (module.js:652:30)

at Object.Module._extensions..js (module.js:663:10)

at Module.load (module.js:565:32)

at tryModuleLoad (module.js:505:12)

at Function.Module._load (module.js:497:3)

#2

Try installing it for the top of the package.json file


#3

have tried - still getting the error


#4

Can you share the url? Or the code for server.js?

Use 3 back ticks to format your code when posting here :slight_smile:


#5

Hope its done right - here is the Server.js code:

 /******************************************************
 * PLEASE DO NOT EDIT THIS FILE
 * the verification process may break
 * ***************************************************/

'use strict';

var fs = require('fs');
var express = require('express');
var app = express();

if (!process.env.DISABLE_XORIGIN) {
  app.use(function(req, res, next) {
    var allowedOrigins = ['https://narrow-plane.gomix.me', 'https://www.freecodecamp.com'];
    var origin = req.headers.origin || '*';
    if(!process.env.XORIG_RESTRICT || allowedOrigins.indexOf(origin) > -1){
         console.log(origin);
         res.setHeader('Access-Control-Allow-Origin', origin);
         res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
    }
    next();
  });
}

app.use('/public', express.static(process.cwd() + '/public'));

app.route('/_api/package.json')
  .get(function(req, res, next) {
    console.log('requested');
    fs.readFile(__dirname + '/package.json', function(err, data) {
      if(err) return next(err);
      res.type('txt').send(data.toString());
    });
  });
  
app.route('/')
    .get(function(req, res) {
		  res.sendFile(process.cwd() + '/views/index.html');
    })

// Respond not found to all the wrong routes
app.use(function(req, res, next){
  res.status(404);
  res.type('txt').send('Not found');
});

// Error Middleware
app.use(function(err, req, res, next) {
  if(err) {
    res.status(err.status || 500)
      .type('txt')
      .send(err.message || 'SERVER ERROR');
  }  
})

app.listen(process.env.PORT, function () {
  console.log('Node.js listening ...');
});



#6

It looks OK to me. Any chance to see the whole project?


#7

sure thing - will this do or you need another view? https://glitch.com/edit/#!/alluring-velociraptor


#8

I sent you a request to join the project so I can see the logs.


#9

Fixed. Something weird on Glitch. I had to set a brand new content for the package.json so it refreshes from scratch and then paste your old code. Nothing wrong with your code but with Glitch itself.


#10

Alright - Thanks for the help! :slight_smile: