Convert exports and module. Exports

Source: Internet
Author: User
81534819 scenario Reproduction

a.js

export let test = function () {  console.log(‘1‘);}
  • 1
  • 2
  • 3

b.js

let a= require (‘./a‘);a.test();
  • 1
  • 2

Runnode bThe following error occurs:

export default {^^^^^^SyntaxError: Unexpected token export
  • 1
  • 2
  • 3
  • 4
Solution

a.jsChange to the following:

exports.test = function () {  console.log(‘1‘);}
  • 1
  • 2
  • 3
Root Cause

Node and browser support different module specifications.

Entries Node Browser
Module specification Commonjs Es6
Export * modules.exports; Exports Export; export default
Introduction Require Import; Require
1. about exports and module. Exports
  • When a node executes a file, it generates an exports and module object for the file,
    The module has an exports attribute.
  • Exports = module. Exports = {};
2. About export and export default
  • Both export and export default can be used to export constants, functions, files, modules, etc.
  • In a file or module, there can be multiple export and import, and export default has only one
  • Export via export. Add {} during import, and export default is not required
  • Export can directly export the variable expression, but export default cannot.

 

Convert exports and module. Exports

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.