1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

モジュール管理システム

Posted at

モジュール管理システムの違い

Untitled.png

CommonJS(モジュール管理システム①)→主にnodejs上で動く。

ESmodule(モジュール管理システム②)→ブラウザでもnodejsでも動く。基本こっちで書く。

モジュールのインポートエクスポートの記法が異なる。

Node.js v18.15.0

CJS

sub.js
//CommonJS(モジュール管理システム)のエクスポート書き方
exports.hello = () => {
    console.log("hello,japan")
}

// const hello = () => {
//     console.log("hello,japan")
// }
// module.exports = {
//     hello
// }
main.js
//CommonJS(モジュール管理システム)のインポート書き方
const myModule = require("./sub")
myModule.hello()

EJS

sub.js
//EJS(モジュール管理システム)のエクスポート書き方
const hello = () => {
    console.log("hello,japan")
}

export {
    hello
}
main.js
//CommonJS(モジュール管理システム)のインポート書き方
import { hello } from "./sub.js"
hello()
package.json
{
    "type": "module"
}
1
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?