Skip to main content

Command Palette

Search for a command to run...

An important tip while using Node JS

Updated
•1 min read•View as Markdown
An important tip while using Node JS
M

I'm a professional UI developer with 4+ years of experience in React and React Native. Loves to setup and build things from scratch.

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:

Screenshot 2020-09-06 at 12.36.43 AM.png

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.

Screenshot 2020-09-06 at 12.27.50 AM.png

Now, if you run your server file:

Screenshot 2020-09-06 at 12.28.04 AM.png

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.

L

Nice tip to know! Thanks for sharing 😉

1