The server does not start via the node index.js , although it starts via the npm start command, it costs babel7 and nodemon . Gives an error message:

 import http from 'http' ^^^^ SyntaxError: Unexpected identifier at new Script (vm.js:79:7) at createScript (vm.js:251:10) at Object.runInThisContext (vm.js:303:10) at Module._compile (internal/modules/cjs/loader.js:656:28) at Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10) at Module.load (internal/modules/cjs/loader.js:598:32) at tryModuleLoad (internal/modules/cjs/loader.js:537:12) at Function.Module._load (internal/modules/cjs/loader.js:529:3) at Function.Module.runMain (internal/modules/cjs/loader.js:741:12) at startup (internal/bootstrap/node.js:285:19) 

Code index.js :

 'use strict'; // 1: NPM dependencies. import http from 'http' import express from 'express' import logger from 'morgan'; import bodyParser from 'body-parser'; import passport from 'passport'; // 2: App related modules. // ... Nothing here, yet! // 3: Initializations. const hostname = '127.0.0.1'; const port = process.env.PORT || 8080; const app = express(); const server = http.createServer(app); const apiRoutes = require('./src/routes/routes').default; // 4: Parse as urlencoded and json. app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: false })); // 5: Hook up the HTTP logger. app.use(logger('dev')); // 6: Hook up Passport. app.use(passport.initialize()); // 7: Set the static files location. // 8: Home route. app.get('/', (req, res) => res.status(200).send({ message: 'Welcome', })); // Bundle API routes. app.use('/api', apiRoutes); // 9: Start the server. server.listen(port, hostname, () => { console.log(`Server running at http://${hostname}:${port}/`); }); 

My package.json :

  ... "version": "1.0.0", "main": "index.js", "scripts": { "start": "nodemon --exec babel-node index.js" }, ... "devDependencies": { "@babel/cli": "^7.2.3", "@babel/core": "^7.2.2", "@babel/node": "^7.2.2", "@babel/preset-env": "^7.3.1" }, "dependencies": { "aws-sdk": "^2.398.0", "bcrypt": "^3.0.3", "body-parser": "^1.18.3", "cron": "^1.6.0", "express": "^4.16.4", "express-validator": "^5.3.1", "generate-password": "^1.4.1", "jsonwebtoken": "^8.4.0", "moment": "^2.24.0", "morgan": "^1.9.1", "multer": "^1.4.1", "nodemailer": "^5.1.1", "passport": "^0.4.0", "passport-jwt": "^4.0.0", "pg": "^7.8.0" }, ... 

Why it happens?

  • Your Noda does not support imports, I think so - Dmytryk February
  • And what kind of support? - MegaRoks
  • "start": "nodemon --exec babel-node index.js" this string just not so? It starts node with parameters where you are using babel . And launching the node index.js module babel will not magically node index.js up. And without babel node does not understand the import instruction - Doigrales
  • That is, you need to change the script to "start": "nodemon --exec babel-node node index.js" . I understand correctly? - MegaRoks
  • @Megaroks not. The node index.js starts the file execution, and npm start runs according to the package.json configuration - Doigrales

0