Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The difference between "require(x)" and "import x"

I've just started working on a small node project that will interface with a MongoDB. However, I cannot seem to get the relevant node modules to import correctly, even though I have installed them correctly via npm.

For example, the following code throws an error, telling me that "express has no default export":

import express from "express"; 

However, this code works:

const express = require("express"); 

So my question is, what is the difference in how the import and variable/require methods function? I'd like to fix whatever is plaguing my imports on the project, as it seems likely to cause additional problems down the road.

like image 839
austinthemassive Avatar asked Oct 11 '17 00:10

austinthemassive


People also ask

What is the difference between require and import?

One of the major differences between require() and import() is that require() can be called from anywhere inside the program whereas import() cannot be called conditionally, it always runs at the beginning of the file. To use the require() statement, a module must be saved with . js extension as opposed to .

Is require or import better?

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.

Can I use require and import both?

Cases where it is necessary to use both “require” and “import” in a single file, are quite rare and it is generally not recommended and considered not a good practice. However, sometimes it is the easiest way for us to solve a problem. There are always trade-offs and the decision is up to you.


2 Answers

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

enter image description here

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.

like image 98
Always Sunny Avatar answered Sep 19 '22 06:09

Always Sunny


The major difference between require and import, is that require will automatically scan node_modules to find modules, but import, which comes from ES6, won't.

Most people use babel to compile import and export, which makes import act the same as require.

The future version of Node.js might support import itself (actually, the experimental version already does), and judging by Node.js' notes, import won't support node_modules, it base on ES6, and must specify the path of the module.

So I would suggest you not use import with babel, but this feature is not yet confirmed, it might support node_modules in the future, who would know?


For reference, below is an example of how babel can convert ES6's import syntax to CommonJS's require syntax.

Say the fileapp_es6.js contains this import:

import format from 'date-fns/format'; 

This is a directive to import the format function from the node package date-fns.

The related package.json file could contain something like this:

"scripts": {     "start": "node app.js",     "build-server-file": "babel app_es6.js --out-file app.js",     "webpack": "webpack" } 

The related .babelrc file could be something like this:

{     "presets": [         [             "env",             {                 "targets":                 {                     "node": "current"                 }             }         ]     ] } 

This build-server-file script defined in the package.json file is a directive for babel to parse the app_es6.js file and output the file app.js.

After running the build-server-file script, if you open app.js and look for the date-fns import, you will see it has been converted into this:

var _format = require("date-fns/format");  var _format2 = _interopRequireDefault(_format); 

Most of that file is gobbledygook to most humans, however computers understand it.


Also for reference, as an example of how a module can be created and imported into your project, if you install date-fns and then open node_modules/date-fns/get_year/index.js you can see it contains:

var parse = require('../parse/index.js')  function getYear (dirtyDate) {   var date = parse(dirtyDate)   var year = date.getFullYear()   return year }  module.exports = getYear 

Using the babel process above, your app_es6.js file could then contain:

import getYear from 'date-fns/get_year';  // Which year is 2 July 2014? var result = getYear(new Date(2014, 6, 2)) //=> 2014 

And babel would convert the imports to:

var _get_year = require("date-fns/get_year");  var _get_year2 = _interopRequireDefault(_get_year); 

And handle all references to the function accordingly.

like image 29
A-yon Lee Avatar answered Sep 20 '22 06:09

A-yon Lee