Example: 2h15 min
Here are topics that should be understood before this topic:
Why it is worth learning this topic
require
and when using import
is very helpful when building a full stack application.When should you use Import instead of Require?
Participants will be able to:
require
and import
With JavaScript, when you want to include code that another developer has produced and made available to the open source community, you now have two main ways to bring in that code into your own script: require
and import
. When starting out, it can sometimes become confusing when to use one or another. So, in order to clarify this a little bit, let’s dive into the core of what they are, how they differ, and when you should use one or the other.
The first thing we need to do is to understand what a JavaScript module is. To put it simply, a module allows you to use chunks of code that you haven’t written in your program. However, if you would like to dig deeper on the subject, I invite you to have a look at the official MDN documentation in the supplemental materials section.
When using Node, if you want to include modules that are in another file, you can use the require
built-in function. It will first read a JavaScript file, then execute it, and finally return the exports
object. More technically, the require
function will look for the specified file in the following order:
Built-in core Node.js modules. For example: fs
Installed NPM Modules. It will look in the node_modules folder. For example: express
Local Modules.
It is important to note that if the specified module name has a ./
, a /
or a ../
, it will look for the directory/file in the given path, matching the .js
and .json
extensions. Moreover, if you want to import a file, you can just use the filename without the extension. For example, if you would like to import code.js
, you could simply do:
For your convenience, here are some examples of popular Node built-in modules: http
, url
, path
and os
. For a more complete list, see this nicely laid out W3School Reference.
In the guided practice below, you will be able to test the require
function.
The import
statement was first introduced in ES6, in 2015. It is a static import statement, which basically means that it will only bring in what has been exclusively and specifically exported from another module.
The syntax is also quite different, as the module path must be a string literal, whereas when we use require
, the module path can be dynamic.
For example, with require
, this will work:
However, due to the static aspect of import
, you cannot do:
You can also import multiple functions with the import
statement. These are called named imports. To do so, you simply include the functions in curly braces.
For example:
You can also import a complete namespace. To do so, you simply use the following syntax:
If you simply import the name of the module, you will get the default export. This is common in libraries such as React:
And, of course, you can combine these:
Finally, you can also refer to an ES Module (if you need more information on modules, see the reference in the supplemental materials).
This means that you can export objects and methods you have created.
For example:
In the guided practice, you will also be able to test using import
.
While there are many differences between require
and import
, which are detailed in the chart below, in my opinion the two major ones are:
import
and export
when it has been configured for you, such as in a create-react-app
app.require
and module.exports
everywhere else.For your convienience, here is a detailed chart of the differences between require
and import
.
REQUIRE | IMPORT |
---|---|
Can be called at any time and place in the program. | Always run in the beginning of the file. |
You can directly run the code with the require statement. | Will not run directly in Node without lots of annoying special setup. |
Can leave out a .js extension when importing a local module | Cannot leave the extension |
To include multiple functions, you first export the functions in an object, then import them using require |
To include multiple functions, simply import them between curly braces. |
require
The first thing we need to to is to create our export file. So, opening your favorite code editor, navigate to a new a folder for this practice and create a file named my-module-for-require.js
. Inside the file, enter the following snippet:
This basically creates something to be exported and then it attaches it to the exports
object.
Then, you need to create the file in which we will call our module. So within the same folder for now, create a file named example-for-require.js
, and then use require
as such:
// require your module
const message = require("./my-module-for-require");
// console log it
console.log(message);
Now, open a terminal, make sure you are inside of the folder where the example-for-require.js
file is and enter the following command to run your program:
If all goes well, you should see:
Awesome! You’ve just created a small module and used it in your own program. I’m sure you can appreciate all the possibilities!
import
As such, it’s not possible to use import
in Node directly. Now, as stated above, we can circumvent this by creating a simple package.json
file to set an environment that will allow us to use import
.
package.json
fileWithout going to much into the details of what is a package.json
file and why we use it, let’s keep things simple and say that this file is like a blueprint of what your app will need to run properly. It can contain many things, depending on the app specificity. When you want to “activate” the blueprint, you simply run npm install
, and then you should see a file called package-lock.json
appear. This file contains details on the installation you have just made, and you can disregard it for now.
Don’t worry if this seems a bit confusing as we just need to use this in order to set the environment and use import
with a .js
file.
First, create a file named my-module-for-import.js
and enter the following code:
export const firstMessage = "This is a the first message!";
export const secondMessage = "This is a the second message!"
Then, create a file named example-for-imports.js
and enter the following code:
import { firstMessage, secondMessage} from './my-module-for-import.js';
console.log(firstMessage);
console.log(secondMessage);
Finally, we need to create the package.json
file and put this code in:
This simply will lay the grounds for our program to run as a module. To test this, inside of the new created folder, we first need to run the install command like I mentioned above:
NPM will run the installation . It is possible that you will see warnings or notices, but don’t mind them as it’s fine for our purposes.
When the installation is done, you can now run:
And you should see:
That’s it! You now know how to use import
with Node!
Please note that the key thing with this is to always stay tuned to changes in the ECMA specifications, as they change often and the codebase progresses to bring in usually more depth and better performance. In my opinion, a good developer is a developer who’s always keen to learn and is on the lookout for these changes!
Now, pat yourself on the back, as you’ve gone through somewhat tough material. Go back, revise, try different things, and when you’re ready, tackle on some of the challenges below!
Using both the require
and import
methods, try exporting something different than the simple Strings we used in the practice, and then import it in your program. It could be a number, it could be an object, it could be a function, etc. How will you call what you have exported? Reflect on the many usage of this and how you could apply this to a real app.
Try a mix of the imports seen for the import
method. Which one do you find more useful? Which one do you think is easier to work with?
Try to create different modules in different paths and call them within the same file.
Try to recreate the summary chart by yourself, see what information you retained.
Make a detailed cheatsheet of the syntax for require
and import
, clearly differentiating between the different imports for the latest.
Difference between require and import in JavaScript - Programmer Sought
Modules - JavaScript for impatient programmers (ES2020 edition)
Webpack: Import vs Require, and why | by Jake Carson Zerrer | untapt Insights
Difference between node.js require and ES6 import and export - GeeksforGeeks
The three differences between require and import in Node.js | by Matt Lim | Medium
What’s New in Node.js 12: ESM Imports | www.thecodebarbarian.com
node.js - The difference between “require(x)” and “import x” - Stack Overflow