이 글은 udemy의 '한입 크기로 잘라 먹는 리액트(React.js)'를 수강하고 적는 글입니다.
📁 index.js
//불러오기
const calc = require("./calc");
console.log(calc.add(1,2));
console.log(calc.sub(10,2));
📁 calc.js
//계산 기능을 하는 파일
const add = (a, b) => a+b;
const sub = (a, b) => a-b;
//내보내기
module.exports = {
moduleName: "calc module",
add: add,
sub: sub,
};
package.json
main: 진입 파일
scripts: 미리 명령어 만들어놓기
dependencies: 어떤 외부 패키지를 설치했는가
📁 package.json{ "name": "package-example1", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1", "start": "node index.js" }, "author": "", "license": "ISC", "dependencies": { "randomcolor": "^0.6.2" } }
실습
npm install randomcolor
: 외부 패키지 설치
📁 index.jsconst randomColor = require('randomcolor'); // import the script console.log(randomColor());
npm start
= npm index.js