I want to create a server in NodeJS using the es6 experimental modules with express but it seems like what was working perfectly for the past days has stop working for no apparent reason. And my code in the past days was much more complicated than what I'm trying to run right now so it's probably something stupid which I forgot.
When I start my server I'm getting the following error but I can't find what is wrong.
> node --experimental-modules src/app.js
(node:30824) ExperimentalWarning: The ESM module loader is experimental.
A:\projects\olympiades\t05\src\app.js:2
import express from 'express';
^^^^^^^
SyntaxError: Unexpected identifier
at new Script (vm.js:51:7)
at createScript (vm.js:136:10)
at Object.runInThisContext (vm.js:197:10)
at Module._compile (module.js:613:28)
at Object.Module._extensions..js (module.js:660:10)
at Module.load (module.js:561:32)
at tryModuleLoad (module.js:501:12)
at Function.Module._load (module.js:493:3)
at createDynamicModule (internal/loader/Translators.js:50:15)
at setExecutor (internal/loader/CreateDynamicModule.js:49:23)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] start: `node --experimental-modules src/app.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
And my src/app.js
code is as simple as this:
'use strict';
import express from 'express';
const app = express();
app.use('/', express.static(process.cwd() + '/views'));
app.set('view engine', 'ejs');
app.get('/', (req, res) => {
res.render('index', {page: 'login'});
});
I tried searching on google but the only results I've got was import
which was undefined and most of them did add --experimental-modules
flag when running js but it's not my case and it's not import
which is undefined but export
.
I also tried without 'use strict';
but same result
I don't understand why node decided to stop working.
Update 1
I forgot to include my version informations so here they are:
- NodeJS: v9.8.0
- NPM: 5.8.0
- express: ^4.16.3
- mongodb: ^3.0.7
- ejs: ^2.5.9
- body-parser: ^1.18.2
- underscore: ^1.9.0
Update 2
I can't add any other modules since I'm doing this to practice for an upcomming event and these are the modules that will be installed during the event. So, in other worlds, I can't use babel or typescript.
Update 3
I tried to open the project which was working earlier and it seems like NodeJS has globally stopped working. I'm gonna try to reinstall NodeJS.
Update 4
Even after a complete uninstall & reinstall of NodeJS, I still get the exact same error.
What the %$#&@ is going on seriously?
What's the next step now? Should I create an issue on GitHub now?