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:


#11


SpaniardDev

Aug 22

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.

I think I’m having the same problem but what do you mean by set a brand new content? delete the file let it update and then paste the code back in? Is it just the package.json or also the server file?

My project boilterplate:
https://glitch.com/edit/#!/shy-pepper

Update:

I tried adding and deleting files, I ended up just forking the project again from the FCC intro link at the beginning of the section and copy pasted my own code back in again and it worked fine… I’d really like to deploy and complete these on github as well.

It works now no change in code just made new project and copy paste my old code:

https://walnut-hen.glitch.me/


#12

Hey,

Yeah, few times Glitch stucks unexpectedly. Glad you fixed it.


#13

I’m having the same problem, I forked a new empty project, pasted my code and I still have the same problem (on code that passed the tests a few days ago!).
First project: https://glitch.com/edit/#!/fcc-aamc-03
Second fork: https://glitch.com/edit/#!/fcc3-retry

Will try to set up a server myself, or I cannot continue to learn…
Update: I cloned the repo on my server and done what was asked on an exercise (again, I passed the exercise already when glitch was working so I know it’s not the code), I am able to launch the app with node start and see the page on my-server-ip:port but fcc can’t accept it apparently as a solution, does it require a glitch project?