ਇਹ JavaScript ਦੀਆਂ ਦੋ ਮਾਡਿਊਲ ਸਿਸਟਮਾਂ ਹਨ ਜੋ ਕੋਡ ਨੂੰ ਫ਼ਾਈਲਾਂ ਵਿੱਚ ਵੰਡਦੀਆਂ ਹਨ।
ES Modules (ESM) — ਆਧੁਨਿਕ ਮਾਪਦੰਡ
// import / export, used in browsers and modern Node
import { sum } from ;
defaultThing ;
x = ;
() {};
ਇਹ JavaScript ਦੀਆਂ ਦੋ ਮਾਡਿਊਲ ਸਿਸਟਮਾਂ ਹਨ ਜੋ ਕੋਡ ਨੂੰ ਫ਼ਾਈਲਾਂ ਵਿੱਚ ਵੰਡਦੀਆਂ ਹਨ।
// import / export, used in browsers and modern Node
import { sum } from ;
defaultThing ;
x = ;
() {};
const { sum } = require("./math");
module.exports = { x: 1 };
| ES Modules | CommonJS | |
|---|---|---|
| Syntax | import/export | require/module.exports |
| ਲੋਡਿੰਗ | static, async | dynamic, synchronous |
| ਵਿਸ਼ਲੇਸ਼ਣਯੋਗ | ✅ → tree-shaking | ❌ ਮੁਸ਼ਕਲ |
| Bindings | live (read-only) | ਨਕਲ ਕੀਤਾ ਮੁੱਲ |
this ਸਿਖ਼ਰ ਤੇ | undefined | module.exports |
Static ਦਾ ਮਤਲਬ ਹੈ ਕਿ ESM imports parse ਸਮੇਂ ਨੂੰ ਜਾਣੇ ਜਾਂਦੇ ਹਨ, ਜੋ bundlers ਨੂੰ tree-shaking (ਨਾ ਵਰਤੇ ਜਾਣ ਵਾਲੇ exports ਨੂੰ ਛੱਡ ਦੇਣਾ) ਕਰਨ ਦਿੰਦਾ ਹੈ। CommonJS require dynamic ਹੈ (ਤੁਸੀਂ conditionally require ਕਰ ਸਕਦੇ ਹੋ), ਇਸ ਲਈ ਇਸਦਾ ਆਸਾਨੀ ਨਾਲ ਵਿਸ਼ਲੇਸ਼ਣ ਨਹੀਂ ਕੀਤਾ ਜਾ ਸਕਦਾ।
{ "type": "module" } // in package.json, or use the .mjs extension
ਤੁਸੀਂ CommonJS ਤੋਂ ESM-only ਪੈਕੇਜ ਨੂੰ require() ਨਹੀਂ ਕਰ ਸਕਦੇ; ਦੋਵਾਂ ਨੂੰ ਮਿਲਾਉਣਾ ਮੁਸ਼ਕਲ ਹੈ। ESM imports ਵੀ live bindings ਹਨ — ਜੇਕਰ exporter ਮੁੱਲ ਨੂੰ ਬਦਲਦਾ ਹੈ, importers ਨਵਾਂ ਮੁੱਲ ਦੇਖਦੇ ਹਨ (CJS ਤੁਹਾਨੂੰ snapshot ਕਾਪੀ ਦਿੰਦਾ ਹੈ)।
ESM ਭਵਿਸ਼ਯਤ ਹੈ (browser-native, tree-shakeable, top-level await)।
ਨਵੇ ਕੋਡ ਲਈ ਇਸ ਨੂੰ ਤਰਜ਼ੀਹ ਦਿਓ; CJS ਨੂੰ ਸਮਝੋ ਕਿਉਂਕਿ Node ਪਾਰਿਸਥਿਤਿਕ ਦਾ ਬਹੁਤਾ ਅਜਿਹਾ ਹੀ ਹੈ।
ਵਿਸਤ੍ਰਿਤ ਜਵਾਬਾਂ ਨਾਲ IT ਇੰਟਰਵਿਊ ਸਵਾਲਾਂ ਦੀ ਇੱਕ ਲਾਇਬ੍ਰੇਰੀ — ਜੂਨੀਅਰ ਤੋਂ ਸੀਨੀਅਰ ਤੱਕ।
ਦਾਨ ਕਰੋ