Javascript

How Import, Export and Require work with Node.JS

You may have seen the following line in JS:

javascript Copy
const fs = require('fs');

And you may have then seen this..

javascript Copy
import fs from 'fs'

And even worse, you may have seen this:

javascript Copy
import { promises as fs } from 'fs'

What does all of this mean?! And why are there so many ways to seemingly import packages in Node.JS? And why can't I get import to work on my Node.JS server? Let's explore what it all means.

Require

Out of the box, when you are writing in pure Javascript in Node.JS, require() works like a charm. That's because require was built for Node.JS specifically. If a file exports something, then require will import that export. Suppose we have a package called 'general' with an index.js file like this:

javascript Copy
export.consoller = function(msg) { console.log(msg); } export.adder = function(x, y) { return x + y; } export.name = 'Some Name';

This format, using export.[function] is NPM specific. It was built to work with NPM, and so is a bespoke part of Node.JS, not aligned to any particular standard. To import those functions, we can easily use require:

javascript Copy
const general = require('general');

Any exports we have, i.e. adder() can now be accessed via general.adder() in our code. This is the most straight forward way add packages into your code.

Import

The difference between import and require is require is for Node.JS, and import is a standard ECMAScript proposal. Import uses a slightly different notation, but allows us to do roughly the same thing that import does.

The import standard gives us a bit more flexibility, and works in such a way that we can import specific pieces of functionality. To start, let's look at a simple example of how you export for an import function:

javascript Copy
const consoller = function(msg) { console.log(msg); } const adder = function(x, y) { return x + y; } const name = 'Some Name'; export { consoller, adder, name }

Now when we import, we can import only part of this module. For example:

javascript Copy
import { consoller } from 'general'

Now we only need to reference consoller, and can reference it as consoller(). If we didn't want that, we could import consoller as something else, i.e.:

javascript Copy
import { consoller as myFunction } from 'general' myFunction()

Why is import not working in Node.JS for me?

Import is a new-ish standard, so it won't work exactly how you expect right out of the box. Make sure you've got at least Node.JS version 12 installed. Then, we need to update our package.json. If you don't have one, run npm init on your command line in the folder you're working in.

Change your package.json to have the line "module":"true", as shown below:

javascript Copy
// ..... "name": "Fjolt", "type": "module", /* This is the line you need to add */ "repository": { "type": "git", "url": "..." }, "author": "", "license": "ISC", // .....

Now modules will work by default in your Node.JS directory. There is a catch though - and that is that now require() will not work - so make sure you've fully converted over to import before making this change.

Why do we have import and require?

So essentially, we have two ways to do roughly the same thing in Node.JS. Why do we need both? require was written originally because import didn't exist, and Node.JS needed a way to insert packages easily. Later on, the group that oversees Javascript's development put forward the proposal for import. In other words, Node.JS wanted to do something fast, so invented their own methodology.

Now that we have import (which is arguably better and more fleshed out than require), I would recommend switching to it if you can. It's standard, so it means you can use it in both frontend and backend development, and it will give you more options for importing and exporting your packages. If you do anything in the frontend, it will also limit file size, by only importing what you need!

Last Updated Tuesday, 27 April 2021
Click to Subscribe Subscribed

Subscribe

Subscribe to stay up to date with our latest posts via email. You can opt out at any time.

Not a valid email