Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize module export and import in node and ES6

Shulou Source: shulou.com Published: 2022-06-01 02:05:10 09月15日 Update

This article mainly introduces "how to achieve module export and import in node and ES6". In daily operation, I believe that many people have doubts about how to achieve module export and import in node and ES6. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "how to achieve module export and import in node and ES6". Next, please follow the editor to study!

Export and Import of node

Export syntax for 1.node

Var path = {} module.exports = path

Import syntax for 2.node

Const path = require ('path')

Export and Import of ES6

Export syntax for 1.ES6

Export default {} (only one member can be exposed) export var s = {} export var b = {} (multiple members can be exposed)

Import syntax for 2.ES6

Import a from 'package name (or file path)' (import export default leak members) import {s} from 'package name (or file path)' (import export leak members with the same name)

Examples add:

/ / ordinary export export {name:'zs', age: 20} / / the exported module defaults to the user's name, and can only be exported once using default. Other modules exported by export default {name:'zs', age: 20} / / export var title = "Little Star" export var content = "ha"

Import instance

Import {name,age} from'. / test.jsimport M1 from'. / test.jsimport {title,content} from'. / test.js'console.log (M1) console.log (title+ "-" + content) so far, the study on "how to implement module export and import in node and ES6" is over. I hope you can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Tags: Modules members grammar learning examples files more paths help practical common consistent next users names multiple small stars articles methods theory Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Apple Shulou Technology macOS Microsoft