The difference between Require/exports and Import/export

Source: Internet
Author: User
the difference between Require/exports and Import/export.

Require/export and Import/export are the concepts that belong to two different norms: Require/exports belong to the COMMONJS specification, Nodejs is the implementation of the specification. Import/export belongs to the ES2015 specification (ES6), which is more normative and authoritative than COMMONJS.

At present, ES6 is mainly converted to ES5 by Babel, so the written import/export is converted to Babel by Require/exports. Both can be viewed as outputting an object and introducing an object and using its properties or methods. difference between

The main difference between Require/exports and Import/export is the form of writing:

Require/exports's writing

const FOO = require ("./foo");
Exports is a reference to Module.exports and is described in detail below.
exports.foo = foo;
Module.exports = foo;

Import/export the writing
import foo from "./foo"
import {foo} from "./foo"
import * as Foo from "./foo"
Export Default foo
export const foo
export * from "foo"
the difference between exports and Module.exports:Exports is a module.exports reference, that is, the exports pointer points to the Module.exports memory address. Require only accept module.exports not accept exports

Exports and Module.exports are both object types, which are reference type data, and the exports memory address and module.exports memory address are the same. The simple thing is that one of them has changed, and the other one has changed.

Exports.foo = "stupid"
console.log (module.exports.foo)
//stupid

//But when the exports point to another object, the situation is different.
At this point the exports memory address points to a new object.
exports = {
    foo: "Stupid"
}

console.log (Module.exports.foo = = Exports.foo)
//false

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.