Using Node.Js Require Vs. Es6 Import/Export

Using Node.js require vs. ES6 import/export

Update

Since Node v12 (April 2019), support for ES modules is enabled by default, and since Node v15 (October 2020) it's stable (see here). Files including node modules must either end in .mjs or the nearest package.json file must contain "type": "module". The Node documentation has a ton more information, also about interop between CommonJS and ES modules.

Performance-wise there is always the chance that newer features are not as well optimized as existing features. However, since module files are only evaluated once, the performance aspect can probably be ignored. In the end you have to run benchmarks to get a definite answer anyway.

ES modules can be loaded dynamically via the import() function. Unlike require, this returns a promise.



Previous answer

Are there any performance benefits to using one over the other?

Keep in mind that there is no JavaScript engine yet that natively supports ES6 modules. You said yourself that you are using Babel. Babel converts import and export declaration to CommonJS (require/module.exports) by default anyway. So even if you use ES6 module syntax, you will be using CommonJS under the hood if you run the code in Node.

There are technical differences between CommonJS and ES6 modules, e.g. CommonJS allows you to load modules dynamically. ES6 doesn't allow this, but there is an API in development for that.

Since ES6 modules are part of the standard, I would use them.

When to use import and when to use require in Node.JS

Summary

import is the future of the Javascript language in both nodejs and the browser and is used in ECMAScript modules (ESM modules) for loading other modules, either statically or dynamically.

require() is the original way that nodejs loaded modules and is used in CommonJS modules. require() is natively supported in nodejs, but not in browsers (though there are some 3rd party libraries that have require-like module loaders for the browser).

Pick One

You will generally want to pick one mechanism and use it for your whole project as it complicates things to mix/match. import had some growing pains for awhile in nodejs, but the implementation is fairly complete now. Either way, you will probably run into some modules that are not of the type you chose for your project and will have to learn how to deal with that.

If I was starting a new project that I expected to last awhile and be working on it for years, I would pick ESM modules with import as that is the present/future architecture for modules in Javascript.

If I was writing a quick "get it done" kind of script that relied on a bunch of scripts that are only available in CommonJS, I would probably use CommonJS require().

If I was writing modules I wanted to share between nodejs and browsers, I'd pick ESM because it is supported in both.

Your Questions

When I look at the documentation for Node.JS for the file system "fs" module it doesn't make any mention of doing the const fs = require('fs); instead it talks about using the import method import { mkdir } from 'fs'

The current version of the fs documentation lets you pick whether you want the code examples to show CJS or ESM module syntax. You were apparently looking at the doc when it was showing ESM syntax. You can switch it. Either will work.

When you see this image Sample Image in the documentation, that lets you toggle the sample code in the doc from CJS to ESM or back.

SyntaxError: Cannot use import statement outside a module

In nodejs, you have to tell nodejs in some way whether your top level module is CJS or ESM. By default, a .js file is treated as CJS and an .mjs file is treated as ESM. If nodejs thinks you have a CJS file, you cannot use import (that is only for ESM modules).

You can also modify the package.json file that controls your particular script (by adding "type": "module" and force your file to an ESM module if you want (which allows you to use .js files as ESM modules).

Using const fs = require('fs'); works just fine

Yes, if nodejs thinks you are using a CJS module.

I want to make sure my code is consistent/correct so which one do I use and why?

See my previous explanation for when I'd choose each module type. If you are just learning, I would suggest you learn how to tell nodejs you're using ESM modules and use import since that is the future direction of the language. require() isn't going to go away anytime soon, but I would not be surprised if we start to see new NPM packages (shareable modules) that only support the newer ESM syntax and can only be loaded from ESM modules. It is possible (with some careful work) to make a shareable module that can be loaded by either an ESM module or a CJS module.

Also, ESM modules are getting some features (such as top-level await) that CJS modules will never get.

More Details

For more details, I suggest reading this article:

Node Modules at War - Why CommonJS and ESM Modules Can't Get Along

The difference between require(x) and import x

This simple diagram will help to you understand the differences between require and import.

Sample Image

Apart from that,

You can't selectively load only the pieces you need with require but with import, you can selectively load only the pieces you need, which can save memory.

Loading is synchronous(step by step) for require on the other hand import can be asynchronous(without waiting for previous import) so it can perform a little better than require.



Related Topics



Leave a reply



Submit