SyntaxError: Cannot use import statement outside a module


So I have taken the course code, and am in the process of using it to build a project of my own. Working on the premise that it will be better than I can write myself, at this time

However, I am currently getting the above issue, which I have tied down to the use of the import commands within /src/server.js

From what I have deduced it is related to a piece of functionality related to a babel dev dependency, something that would create .babelrc… I would prefer not to install every dev dependency without a broad understanding of what it does.

If you can let me know how it’s what babel is doing in this case, to allow the import to work :slight_smile:

Otherwise I may just import the who thing, but that doesn’t potentially help in the long run Or maybe recode the imports to requires.

PS - Why was there a swap from require to import in the code?

1 Like

Hi @NeilM,

Babel is a toolchain that is mainly used to convert ECMAScript 2015+ code into a backward-compatible version of JavaScript in current and older browsers or environments.

Absolutely, you should not install anything that you don’t require in your project.

The babel plugin transforms ECMAScript modules to CommonJS, and hence it will transform the import syntax into require syntax under the hood.

I would suggest you to just spin up a new Node.js server from scratch, and once you are able to implement a very basic hello world function, you can start adding some dependencies like mongodb driver, express js router, and apparently, you’ll be able to figure out what other dependencies you need.

In case you have any doubts, please feel free to reach out to us.

Thanks and Regards.
Sourabh Bagrecha


I had started to think the same.

There is a lot of extra stuff that I probably don’t need at the moment, which is getting in the way of applying what I have learnt on this course.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.