As you might already know that import
and export
statements are not supported natively in nodeJS right now.
This is what happens if you try running your server code with import statements:
So currently, we use require
to do that job of importing node JS modules like express:
const express = require('express');
But now, node has introduced import and export statements in experimental features in versions greater than13.x.x
, where you can easily turn them on by two ways:
1) Setting type: "module"
in package.json
.
Now, if you run your server file:
2) Saving files with extension.mjs
For example:
Save your file with name server.mjs
and run it as:
node server.mjs
That will work too.