GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Check if a File Exists in Node.js
  • Working with UUID in Node
  • Using bcrypt-js to Hash Passwords in JavaScript
  • Working with the Node.js assert Library
  • Modify Authorized redirect_uris For Google OAuth
  • Fundamentals
  • Node
  • Vue
  • Webpack
  • Axios
  • Mongoose
  • Express
  • Lodash
  • npm

This view

  • Original page
  • New address

GeezerVison is showing masteringjs.io — Import vs Require in Node.js

Mastering JS Logo
Mastering JS
Tutorials Newsletter eBooks Jobs
☰
Tutorials Newsletter eBooks Jobs
Tutorials / Node /

Import vs Require in Node.js

Jun 26, 2020

Node.js introduced support for the import statement in Node.js 12, although you need to opt in by setting a package.json configuration option. However, Node.js has no plans to drop support for require() (CommonJS). Which should you use?

Below is a high-level summary of the tradeoffs:

  1. ESM import is part of the JavaScript language spec, require() is not.
  2. import requires a special configuration option in package.json.
  3. import does not support importing JSON files. You'll get a Unknown file extension ".json" error if you try to import a file that ends in .json.
  4. Even though ESM modules work in both the browser and Node.js, there's no guarantee that your Node.js code will work in the browser and vice-versa.
  5. Several Node.js features don't work with ESM: NODE_PATH, __dirname, __filename, and require.extensions don't work if you opt in to { "type": "module" }.

Recommendations

Even though there are numerous tradeoffs, none of the tradeoffs is sufficiently important for us to recommend using one or the other in all cases. Here's a few reasons why you might prefer one over the other:

  1. If you're building a full-stack application and want to use the same syntax all the way through, using ESM imports is a good choice.
  2. If your app uses __dirname for relative file paths with fs, you need to use require() or refactor your app.
  3. If you rely on importing JSON files (configuration, seed data, etc.) you need to use require(), or refactor your app to instead read JSON files using fs.

For now, Mastering JS will stick to using require(), because that's what we're used to and we don't know of a compelling enough reason to switch.


Did you find this tutorial useful? Say thanks by starring our repo on GitHub!

More Node Tutorials

  • Getting Started with Oso Authorization in Node.js
  • Check if a File Exists in Node.js
  • How To Fix "__dirname is not defined" Error in Node.js
  • Working with UUID in Node
  • Using bcrypt-js to Hash Passwords in JavaScript
  • Working with the Node.js assert Library
  • Modify Authorized redirect_uris For Google OAuth
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.