Search code examples
node.jsexpressmongooseimportbabel-preset-env

node not recognizing express even though it is installed


Something that previously worked is no longer working and I'm stumped as to how to fix this. I am running on Mac and have the following versions: Node: v10.14.1 npm: 6.9.0 express: ^4.16.4 @angular/cli: ^7.3.3 babel-preset-env: ^1.7.0

I am getting the following error message even though I have both Express and babel-preset-env installed:

import express from 'express';
       ^^^^^^^

SyntaxError: Unexpected identifier

This is what I have in server.js file:

import express from 'express';
import path from 'path';
import bodyParser from 'body-parser';
import logger from 'morgan';
import mongoose from 'mongoose';
import SourceMapSupport from 'source-map-support';
import cors from 'cors';

// import routes
import weddingRoutes from './routes/wedding.server.route';

// define our app using express
const app = express();

// allow-cors
app.use(function(req,res,next){
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  next();
})
// configure app
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static(path.join(__dirname, 'public')));

// var cors = require('cors');
app.use(cors());

// set the port
const port = process.env.PORT || 3001;

// connect to database
var mongoDB = 'mongodb://127.0.0.1/wedding-website-app';

mongoose.connect(mongoDB, {
  useNewUrlParser: true
});

mongoose.Promise = global.Promise;

var db = mongoose.connection;
db.on('error', console.error.bind(console, 'MongoDB connection error:'));

// add Source Map Support
SourceMapSupport.install();

app.use('/api', weddingRoutes);

app.get('/', (req,res) => {
  return res.send('Api working');
})

// catch 404
app.use((req, res, next) => {
  res.status(404).send('<h2 align=center>Page Not Found!</h2>');
});

// start the server
app.listen(port,() => {
   console.log(`App Server Listening at ${port}`);
});

Solution

  • I tried your code and it showed the error you described until I installed babel-node:

    sudo npm install -g @babel/node
    

    and then used:

    babel-node server.js
    

    For mor information about babel-node check here: babel-node