Interpret files as modules in Node 13.2.0+

Sami C.
1 min readMay 9, 2020

--

Did you ever try something like this in a .js file:

import ReactHtmlParser from 'react-html-parser';

and you ran

node file.js

and got the following error:

SyntaxError: Cannot use import statement outside a module

Well, this is now possible with node 13.2.0+

Simply add the following in your package.json:

"type": "module"

Now all your JavaScript files are interpreted as modules and you can now use the import statement instead of require which is from CommonJS.

Enjoy!

--

--

Sami C.
Sami C.

Written by Sami C.

Freelance Software Engineer

No responses yet