1
votes

I want to setup a server with Express and Body-parser. I did :

npm init -y

in the directory of my project.

then:

npm install express body-parser --save

The result: package.json file as follow

{
  "name": "myapp",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "body-parser": "^1.18.2",
    "express": "^4.16.2"
  }
}

and after that created index.js file where I put this code:

const express = require(‘express’);
const bodyParser = require(‘body-parser’);
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.listen(3000, () => console.log(‘Webhook server is listening, port 3000’));

and run node index.js

I got this error:

(function (exports, require, module, __filename, __dirname) { const express = require(‘express’); ^

SyntaxError: Invalid or unexpected token at createScript (vm.js:80:10) at Object.runInThisContext (vm.js:139:10) at Module._compile (module.js:607:28) at Object.Module._extensions..js (module.js:654:10) at Module.load (module.js:556:32) at tryModuleLoad (module.js:499:12) at Function.Module._load (module.js:491:3) at Function.Module.runMain (module.js:684:10) at startup (bootstrap_node.js:187:16) at bootstrap_node.js:608:3

What's the problem? I didn't understand, it's the first time I use node.js

2
Just a typo or copy/paste related thing? Change ‘express’ to 'express' and so on...pzaenger
also for body-parser?SarahData
Are the inverted commas correct in require('express')?Rishikesh Dhokare
I corrected as @pzaenger told me and it worked! The tutorial I followed caused to have the wrong brackets ..SarahData

2 Answers

4
votes

Change and to single quotes ' and it'll work:

const express = require('express');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.listen(3000, () => console.log('Webhook server is listening, port 3000'));

Run through console:

$ node index.js
Webhook server is listening, port 3000
1
votes

It seems that you've used some text processor (such as MS Word) instead of a simpler text editor (such as Notepad or Wordpad that are available on Windows) or an editor specialized for programmers (such as Nodepad++ or Atom). Because text processors are unaware you are writing a programming code, they think you are writing an ordinary essay to be readable by humans, so they automatically replace straight quotes ' and ' with smart quotes ‘ and ’ that are treated as if they were not quotes at all by node.js. So the same code with single straight quotes actually works fine:

const express = require('express');
const bodyParser = require('body-parser');
const app = express();

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.listen(3000, () => console.log('Webhook server is listening, port 3000'));

I highly recommend using of text editors meant for programmers, especially Atom which is itself designed in node.js for writing apps in node.js and related technologies (html, json, CSS). But if you continue to use MS Word (my version is 2010), the autocorrect option for turning straight quotes into smart quotes by following these menus:

File ↦ Options ↦ Proofing ↦ AutoCorrect options... ↦ AutoFormat ↦ Replace

and ☑ ❏ uncheck the first option.