The error `Syntaxerror: unexpected token ‘export’` will be encoutered when you try to use the export keyword in a version of nodejs that is prior to v14.13.0. To solve the problem, you have to either use the older method of exporting the method, and that is using the method module.exports, or, you may be better off updating to the latest version of nodejs. I recommend you do the latter as it’s better for your system security and number of features. You don’t want to stay behind using old cumbersome method of doing things in nodejs.
Explanation
The error says that the export token was unexpected and that means the Javascript runtime parser has not been able to understand the export keyword. To understand, how does this error happen. We have to understand first what is NodeJS and how it imports and exports modules.
What is NodeJS?
If you are already coding on the client side, you are necessarily using JavaScript. Why? Because all browsers are equipped with a Javascript engine that will allow us to translate our Javascript code into machine code.
Well, one day, there was a guy called Ryan Dahl who had the brilliant idea of taking the Javascript V8 engine, which is the one found in the Chrome browser, and using it outside the Navigator. He created the Node.js platform!
Node.js is very frequently used to write server-side services called APIs (Application Programming Interface) And in the end, we can say that Node.js represents an alternative to server languages like PHP, Java or Python. This alternative has become popular in a lot of companies and has even revolutionized a lot of things in big companies like PayPal. PayPal, which used Java, decided to test Node.js on a small part of its code and the results were quite impressive.
Some real-time applications may have application features that are used in multiple places in that application, but are not available in the NodeJS module repository. To take advantage of reuse in this scenario, you need to create a new NodeJS module. It is not enough to create a new module to be used by other modules in the system. This module must be exported to be reused in other modules. If you’re a UI or Java developer, you’re probably familiar with it. If your Java-based application contains generic or reusable components, develop it as a separate project, create a Jar file, and add it to your project’s required classpath. Don’t worry too much about creating NodeJS modules at this point. We will discuss how to create our own NodeJS modules in the future.
How to import and export modules in NodeJS?
A “module” is any file composed of Javascript code imported into another file or script.
Modules allow you to split up your code to better organize it. In practice, when several people are working on a project, it is common to split the code into several files containing related parts of the final program to facilitate code, maintenance, and overall teamwork.
To export functions or classes, we have to define function or class first and then assign the class or function to a member of the variable exports. Open you Visual Studio code and create new folder for doing this mini-project. Create a new file and name mymodule.js. And type the following inside it:
function add(a,b) {
return a + b;
}
class User {
sayHello() {
console.log("Hello, I'm user!")
}
}
exports.add = add
exports.User = User
After you are done doing that you have to next create another Javascript file and name it index.js.
To import these functions and classes in index.js script we have to use the require function which is compatible with CommonJS version we are using at the moment.
You have to assign the return variable from the require to a variable inside your script to use. And for the require() function, you have to pass the path of the file without the file extension at the end.
const { User, add } = require("./mymodule")
// Or you can do it this way
const mymodule = require("./mymodule")
// The class will be available at mymodule.User and the add function will be available at mymodule.add
The Solution
Now that we know everything regarding how to fix this issue, we will resolve the issue now. There are two main methods for fixing this. First and I advise you do this instead is upgrade to the latest version of NodeJS and the second method is to use CommonJS method of importing function and classes. We will look at each of them right now.
Solution one
IIf you are using are using Windows, download the latest package of NodeJS and install it. And if you are using Linux, I advise you install the global package ‘n’. This package will install everything. After that close and reopen the terminal and the new version will take effect.
First, let’s install n.
npm install -g n
After it complete installing. Run the following command:
n latest
After that, the new package will be completely installed. Now close the terminal session and reopen it. And run the script again.
This will resolve the import errors and make the script function properly.
Solution two
Another simple solution is to use the old way of exporting modules, using CommonJS. To import a module in CommonJS, we use the require function and we give the path to the script without the file extension. The file extension is .js.
class User {
sayHello() {
console.log("Hello, I'm user!");
}
}
module.exports.User = User
After this the export will work fine.
The Conclusion
Thanks for reading the tutorial and sticking all the way to the end. To sum up, there are two possible fixes for this problem. The best one is to update you NodeJS environment and use the latest method of exporting and importing scripts. The other method is to use the way of doing things and use CommonJS syntax by assigning the classes and functions to the exports variable, then importing them with the require() function.
That’s it for this tutorial. If you run into any issues, don’t hesitate to leave a comment.