node 모듈 사용하기, 내장 객체 사용하기

Yeon Jeffrey Seo·2021년 9월 29일
0

Node.js

목록 보기
1/4

모듈 import 하기

ES Modules

import crypto from 'crypto';

CommonJS

const crypto = require('crypto');

npm 사용 중인 경우, 프로젝트 내에서 두 가지 방법 중 1가지만 선택해서 사용가능하다.
default의 경우 CommonJS를 불러오는 방식이다. import를 사용하려면 package.json에 아래 속성을 추가해 준다.

{
	"type" : "module"
}

이 상태에서 require를 사용하고 싶다면, 파일 명을 .cjs로 변경해준다.

node 내장 객체 filename, dirname 사용 불가

참고 자료

https://nodejs.org/api/esm.html#esm_import_meta_url

profile
The best time to plant a tree was twenty years ago. The second best time is now.

0개의 댓글