export& import 모듈화

export default ~이름
import ~별칭 from ~경로

op1.js

const op1 = 50

export default op1

op2;.js

export const op2 = 20;

sumOperation.js

import operation from './op1.js'
import oper1 from './op1.js'
import { op2 } from './op2;.js';


console.log(oper1+op2);
console.log(operation+op2);

package.json (프로그래머가 직접 작성하는 것이 아닌 node js 명령어를 통해 생성 후 변경하고 사용해야 한다!)

{
  "name": "temp",
  "version": "1.0.0",
  "description": "",
  "main": "sumOperation.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "type": "module",
  "keywords": [],
  "author": "",
  "license": "ISC"
}

+ npm install 이 안먹는다면
npm init -y 로 초기세팅 및 package.json 생성 후  "type": "module", 을 추가 해주면 된다.

export ~이름
import {~이름} from ~경로 물론 as 키워드를 통해 별칭을 지정하는 것이 가능하다 
import {~이름 as ~별칭} from ~ 경로
import * as ~별칭 from ~경로 를 통해 해당 경로의 모든 export 된 것들을 한번에 가지고 오는 것 또한 가능하다 

+ Recent posts