node -v
v18.20.5
yarn -v
1.22.22
npm -v
10.8.2
설치 정보는 위와 같다.
인프런에 있는 Vue3 강좌를 따라 하다가 아래와 같은 문제가 발생했다.
ERROR Error: The project seems to require yarn but it's not installed.
Error: The project seems to require yarn but it's not installed.
at checkYarn (...\vue3-form\node_modules\@vue\cli-shared-utils\lib\env.js:46:43)
at exports.hasProjectYarn (...\vue3-form\node_modules\@vue\cli-shared-utils\lib\env.js:42:10)
at ...\vue3-form\node_modules\@vue\cli-service\lib\commands\serve.js:330:34
at Hook.eval [as callAsync] (eval at create (...\node_modules\tapable\lib\HookCodeFactory.js:33:10), <anonymous>:44:1)
... (webpack, tapable 내부 호출 생략)
error Command failed with exit code 1.
yarn은 global로 설치되어 있고 다른 패키지와 충돌이 난 것도 아닌 것 같은데, yarn serve로 실행하면 계속 정상적으로 실행되지 않았다. 그런데 npm으로 실행하면 정상적으로 뜨고 yarn으로 실행할 때만 문제가 생겼다. 뭔가 설정이 누락됐거나 꼬인 것 같았다.
시도한 방법
ChatGPT와 같이 찾아본 일반적인 해결 방법은 아래와 같다.
- yarn 캐시 클린 후 실행 (해결 X)
- env.js에 process.env.VUE_CLI_TEST = true 추가 (해결 X)
- yarn.lock 파일 제거 후 다시 yarn install (해결 X)
3번까지 해도 같은 문제가 계속 나서 잘 풀리지 않았는데, 3번을 하고 나서 2번을 진행하니 정상적으로 실행되었다. env.js 파일 위치는 아래와 같다.
Vue 프로젝트 폴더 > node_modules > @vue > cli-shared-utils > lib > env.js
아무래도 뭔가 꼬였던 것 같다. 3번, 2번 순서로 진행한 뒤 재부팅했더니 정상적으로 실행된다.
오류가 나는 위치를 코드로 확인하기
스택 트레이스에 나온 env.js 46번째 줄과 serve.js 330번째 줄을 @vue/cli-shared-utils 5.0.8, @vue/cli-service 5.0.8 소스에서 찾아봤다. 줄 번호가 오류 메시지와 그대로 맞는다.
// @vue/cli-shared-utils/lib/env.js
exports.hasYarn = () => {
if (process.env.VUE_CLI_TEST) {
return true
}
if (_hasYarn != null) {
return _hasYarn
}
try {
execSync('yarn --version', { stdio: 'ignore' })
return (_hasYarn = true)
} catch (e) {
return (_hasYarn = false)
}
}
exports.hasProjectYarn = (cwd) => {
if (_yarnProjects.has(cwd)) {
return checkYarn(_yarnProjects.get(cwd))
}
const lockFile = path.join(cwd, 'yarn.lock')
const result = fs.existsSync(lockFile)
_yarnProjects.set(cwd, result)
return checkYarn(result)
}
function checkYarn (result) {
if (result && !exports.hasYarn()) throw new Error(`The project seems to require yarn but it's not installed.`)
return result
}
// @vue/cli-service/lib/commands/serve.js (330번째 줄 근처)
if (isFirstCompile) {
isFirstCompile = false
if (!isProduction) {
const buildCommand = hasProjectYarn(api.getCwd()) ? `yarn build` : hasProjectPnpm(api.getCwd()) ? `pnpm run build` : `npm run build`
console.log(` Note that the development build is not optimized.`)
console.log(` To create a production build, run ${chalk.cyan(buildCommand)}.`)
}
...
}
순서대로 보면 이렇다. 첫 컴파일이 끝나면 serve는 "프로덕션 빌드는 yarn build로 하라"는 안내 문구를 찍으려고 hasProjectYarn을 부른다. 프로젝트에 yarn.lock이 있으면 Node 프로세스 안에서 yarn --version을 실행해 보고, 이 명령이 실패하면 yarn이 없다고 판단해서 오류를 던진다. 터미널에서 yarn -v가 잘 나와도 serve를 띄운 프로세스의 환경(PATH 등)에서 yarn을 못 찾으면 같은 오류가 난다.
VUE_CLI_TEST는 원래 Vue CLI 자체 테스트에 쓰는 환경 변수다. 값이 있으면 hasYarn이 yarn을 실행해 보지도 않고 바로 true를 돌려준다. 2번 방법은 이 확인 단계를 통째로 건너뛰게 만든다.
직접 재현해 보기
지금 쓰는 PC에는 yarn이 설치되어 있지 않아서 같은 상황을 만들 수 있었다. 위 env.js를 그대로 가져와서 yarn.lock이 있는 폴더(proj-yarn)와 없는 폴더(proj-npm)에 hasProjectYarn을 호출해 봤다.
// check.js
const path = require('path')
const env = require('./lib/env') // @vue/cli-shared-utils 5.0.8의 env.js
for (const dir of ['proj-npm', 'proj-yarn']) {
try {
const r = env.hasProjectYarn(path.join(__dirname, dir))
console.log(dir, '-> hasProjectYarn =', r)
} catch (e) {
console.log(dir, '-> Error:', e.message)
}
}
$ node check.js
proj-npm -> hasProjectYarn = false
proj-yarn -> Error: The project seems to require yarn but it's not installed.
$ VUE_CLI_TEST=true node check.js
proj-npm -> hasProjectYarn = false
proj-yarn -> hasProjectYarn = true
yarn.lock이 없는 프로젝트는 npm 프로젝트로 보고 넘어가고, yarn.lock이 있는 프로젝트에서만 오류가 난다. VUE_CLI_TEST를 주면 오류 없이 통과한다.
내 환경에서 확인하는 방법
Vue CLI가 하는 확인을 그대로 따라 하려면 프로젝트 폴더에서 아래 명령을 실행해 보면 된다. 버전이 찍히면 정상이고, 오류가 나면 Node에서 yarn을 찾지 못하는 상태다.
node -e "require('child_process').execSync('yarn --version', { stdio: 'inherit' })"
yarn serve로 실행할 때만 문제가 생긴다면 yarn이 띄운 환경 안에서 확인해야 한다. package.json에 확인용 스크립트를 잠깐 넣고 yarn으로 실행하면 된다.
"scripts": {
"check-yarn": "node -e \"require('child_process').execSync('yarn --version', { stdio: 'inherit' })\""
}
yarn check-yarn
주의할 점
node_modules 안의 env.js를 고친 내용은 패키지를 다시 설치하면 사라진다. 파일을 고치지 않고 실행할 때만 환경 변수를 주는 방법도 있다.
# PowerShell
$env:VUE_CLI_TEST = "true"
yarn serve
# cmd
set VUE_CLI_TEST=true
yarn serve
다만 VUE_CLI_TEST는 테스트용 값이라 serve의 다른 동작도 바꾼다. 같은 serve.js에서 개발 서버의 진행률(progress) 표시를 끄는 조건으로도 쓰인다. yarn을 쓰지 않는 프로젝트라면 yarn.lock을 지우고 npm으로만 실행해도 이 확인을 타지 않는다.
그리고 Vue CLI는 지금 유지보수 모드라서, 새 프로젝트는 Vite 기반의 create-vue로 만드는 것을 공식 문서에서 권장한다.
댓글
GitHub 계정으로 로그인하면 댓글과 공감을 남길 수 있습니다.