LoginSignup
0
0

ESMとCJSのimportとexport

Posted at

CJSとESMのexportとimport

CJSの場合のExport

一つのモジュールのエクスポート

sample-1.js
const someFunc () => {
    //do something
}

module.exports = somoFunc

複数のモジュールのエクスポート

sample-1-2.js
const someFunc () => {
    //do something
}
const hoge ="hoge"

// その1 まとめてexport
module.exports = {
    culc: someFunc
    hoge: hoge
}
// その2 一つひとつエクスポート
exports.someFunc = () => {
    //do something
}
exports.hoge = "hoge"

ESMの場合のExport

一つのモジュールをエクスポートする場合

sample-2.js
//デフォルトのエクスポート その1
expost default function somoFunc (){
    //do somothing
}
sample-3.js
//デフォルトのエクスポート その2
function somoFunc (){
    //do somothing
}

expost default somoFunc

ひとつのモジュールのインポート

import.js
import somoFunc from 'sample-2.js'

複数のモジュールをエクスポートする場合

sample-4.js
export function add(x, y) {
  return x + y;
}
export function multiply(x, y) {
  return x * y;
}

複数のモジュールのインポート

sample-5.js
import { add, multiply } from 'sample-4.js'

[参考記事]

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0