33

I have noticed a bit of switching between using const and import for referencing libraries in node.js applications using es6 syntax with Babel.

What is the preferred method and what is the difference between using const and import? Assuming you may be importing the same library in many files/components.

const

const React = require('react')

import

import React from 'react'

Here are the definitions of each but I am still not sure which to use.

import

The import statement is used to import functions, objects or primitives that have been exported from an external module, another script, etc.

const

The const declaration creates a read-only reference to a value. It does not mean the value it holds is immutable, just that the variable identifier cannot be reassigned.

svnm
  • 22,878
  • 21
  • 90
  • 105
  • 1
    The second comes from the standard (ES2015), the first does not. – zerkms Jan 04 '16 at 23:13
  • The `import` syntax is the new *native* ES2015 syntax for the JavaScript module system. – Pointy Jan 04 '16 at 23:14
  • 2
    What you're really asking is what the difference between Node's require and ES2015 import is, which makes this a [duplicate](http://stackoverflow.com/questions/31354559/using-node-js-require-vs-es6-import-export) – adeneo Jan 04 '16 at 23:16
  • No I am wondering if using const has any benefit to just importing it, and the reasoning behind some libraries using the const approach, which I have seen recently instead of import – svnm Jan 04 '16 at 23:20
  • 1
    There is no benefit. – zerkms Jan 04 '16 at 23:22
  • 1
    `const` is just like `var`, the big difference is that a "constant" is read-only, it has nothing to do with imports or requires ? – adeneo Jan 04 '16 at 23:23
  • okay thanks for that, it must be some confusion in the libraries that I have been using switching over to the const approach for no reason. Just a few react and redux libraries and examples are using this approach which is why I wondered if there was some benefit to it. – svnm Jan 04 '16 at 23:26
  • 1
    The benefit is that the variable holding the module can't be changed, it's read-only – adeneo Jan 04 '16 at 23:26
  • 1
    @adeneo an imported identifier cannot be changed either – zerkms Jan 04 '16 at 23:27
  • @zerkms - I'll take your word for it, don't have Babel or anything else set up to test modules atm, and I've never tried changing something that was imported, I think? I always assumed that an ES2015 import could be changed, unless you explicitly did `export const fn = function() {..` etc. – adeneo Jan 04 '16 at 23:53
  • @adeneo http://www.ecma-international.org/ecma-262/6.0/#sec-createimportbinding "The concrete Environment Record method CreateImportBinding for module Environment Records creates a new initialized immutable indirect binding for the name N." As of the `export const` - the `const` modifier affects the declared identifier only. Like `const a = 1; let b = a;` - the former `const a` does not make `b` constant. – zerkms Jan 04 '16 at 23:58
  • @zerkms - you might as well post a link to the Illiad in Greek, I usually grok the spec, but that is just to technical for me, I see the word "immutable" though I have no idea what specifically is immutable, or how that affects actually changing the imported methods. – adeneo Jan 05 '16 at 00:04
  • @zerkms - finally found an [explanation](http://exploringjs.com/es6/ch_modules.html#sec_imports-as-views-on-exports) I understand, and didn't know that, I assumed imports could be changed, or even overwritten, but they are, to quote that article, "live read-only views of the export", which in my opinion sucks balls. – adeneo Jan 05 '16 at 00:14

2 Answers2

24

What is the preferred method and what is the difference between using const and import?

In 2016 it makes sense to stick with the import since that's the part of the standard.

There is no technical reason to prefer import over require though: everything that can be done using require can be done with import and vice versa. In some cases one will be more concise, in another - the other.

To summarise: choose the one that fits the project code conventions/consistency.

zerkms
  • 249,484
  • 69
  • 436
  • 539
  • Thanks, I will be using `import` then. I was just confused as some redux libraries and examples were using the `const` approach I think for the perceived benefit that it would be read-only. As you mentioned in the comments, **an imported identifier cannot be changed** which means there is no advantage. Nice links provided by yourself [here](http://ecma-international.org/ecma-262/6.0/#sec-createimportbinding) and @adeneo [here](http://exploringjs.com/es6/ch_modules.html#sec_imports-as-views-on-exports) help clarify it – svnm Jan 05 '16 at 01:09
  • 2
    The main benefit of the `const` approach is that it works in Node 4/5 without transpiling, which is likely why you saw it in those. – loganfsmyth Jan 05 '16 at 04:41
1

Of course the main difference is older require vs newer 'import', (as it can be var instead of 'const', does not matter much).

As both are require and import are both valid and supported, some developers may wonder which to use. And generally it is better agree within team to use newer 'import' with strict mode and and exact other ES feature.

Pro and Cons opinions are in Using Node.js require vs. ES6 import/export

Paul Verest
  • 60,022
  • 51
  • 208
  • 332