-
Notifications
You must be signed in to change notification settings - Fork 6
절대경로 문제 해결
박재윤 edited this page Nov 26, 2020
·
1 revision
백엔드를 실행시키는데 두가지 명령어를 사용한다.
- 자바스크립트로 build를 해서 실행시키는
npm start
- ts-node를 이용해서 실행시키는
npm run dev
그런데 절대경로를 이용하기 위해서 tsconfig.json
파일에 path를 다음과 같이 설정하고 있었다.
// tsconfig.json
{
"complilerOptions": {
"outDir": "./build",
"baseUrl": "./src",
"paths": {
"@/*": [
"./*"
]
},
}
}
그런데 npm start에서는 base url이 ./build
가 되어야 했다. 따라서 npm run dev
명령을 통해서 실행을 하면 base url이 알맞게 되어 있어서 절대경로에서 오류가 나지 않았지만 npm start
명령어에서는 절대경로를 이용하면 ./src
안에서 모듈을 찾기 때문에 에러가 생겼다.
이를 해결하기 위해서 tsconfig-paths-bootstrap.js
라는 모듈을 만들고 npm start 명령어를
tsc && set PRODUCTION=true && node -r ./tsconfig-paths-bootstrap.js build/index.js
이렇게 바꿔주었다.
tsconfig-paths-bootstrap.js
는
const tsConfig = require('./tsconfig');
const tsConfigPaths = require('tsconfig-paths');
const baseUrl = process.env.PRODUCTION ? './build' : './src';
console.log('base url: ', baseUrl);
tsConfigPaths.register({
baseUrl,
paths: tsConfig.compilerOptions.paths,
});
위와 같아서 PRODUCTION이면 baseUrl을 ./build
로, 아니면 './src'로 해주었다.