import and export in JavaScript | Node.js export default | npm start and package.json #kody_az
Dərs haqqında
Node.js-də import və export kodu müxtəlif fayllara bölüb onları modul kimi idarə etmək üçün istifadə olunur.
Export ilə bir fayldakı dəyişənləri, funksiyaları və ya class-ları “başqa fayllar da istifadə etsin” deyə xaricə açırsan.
Import isə həmin export olunanları başqa fayla gətirib istifadə etməyə imkan verir ki, kod təkrarı olmasın.
Node.js-də müasir ES Modules rejimini işlətmək üçün adətən package.json-da "type": "module" yazılır.
ES Modules-də named export nümunəsi export const x = 1; kimidir və onu import { x } from "./file.js"; ilə gətirirsən.
Əgər faylın “əsas bir çıxışı” varsa, default export istifadə edilir, məsələn export default function() {}.
Default export import ediləndə {} olmur: import something from "./file.js";.
Named import isə {} ilə yazılır və adlar export olunan adlarla dəqiq eyni olmalıdır.
Node.js ES Modules-də çox vaxt import yolunda .js uzantısını yazmaq lazımdır, məsələn ./utils.js.
Import/export istifadə etmək layihəni daha səliqəli, oxunaqlı və böyüdükcə daha rahat idarə olunan edir.
====
In Node.js, import and export are used to split your code into separate files called modules.
With export, you decide which variables, functions, or classes from a file can be used in other files.
With import, you bring those exported things into another file so you don’t rewrite the same code.
Node.js supports modern ES Modules when you enable it (for example with "type": "module" in package.json).
In ES Modules, you can use named exports like export const x = 1; and then import them with import { x } from "./file.js";.
You can also use a default export when a file has one main thing to export, like export default function() {}.
Default exports are imported without curly braces, like import something from "./file.js";.
Named imports use curly braces and the names must match the exported names exactly.
In Node.js ES Modules, you often need to write the file extension in the import path, such as ./utils.js.
Using modules makes projects cleaner, easier to maintain, and easier to scale as the codebase grows.
#kody_az #kodyaz #js #javascript #nodejs #es6 #viral #viralcode #nodejs #viralvideo #random #code #coder #azerbaycan #azerbaijan #baku #baki #programming #programmer #codes #hazırlıq #hazirliq #kurs #kurslar #course #courses #online #onlinecourses #test #quiz