typescript
TypeScript: file already exists, symlink & tsc command not found solutions
2022-01-10
A quick note to gather the solution for solving "file already exists, symlink" and "tsc command not found".
Howdy👋 I'm Arisa, a DevRel Engineer at Storyblok.
I got a trap while I was installing TypeScript and couldn't find the easy, short and clear solution in English sources.
Here's a quick note if you're also suffering from a headache from the 2 errors below.
Error 1:
npm ERR! code EEXIST
npm ERR! path /Users/username/.npm-global/bin/tsc
npm ERR! EEXIST: file already exists
npm ERR! File exists: /Users/username/.npm-global/bin/tsc
npm ERR! Remove the existing file and try again, or run npm
npm ERR! with --force to overwrite files recklessly.
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/username/.npm/_logs/2022-01-10T22_09_15_651Z-debug.log
Or this one.
Error 2:
zsh: command not found: tsc
The environment
Here is the environment I had.
node: v16.3.1
npm: 8.1.2
typescript: 4.5.4
MacOS Big Sur 11.6
Why did I get these 2 errors?
It's nothing complicated. In fact, I was just trying to install TypeScript for the first time to start using it.
$ npm i typescript -g
That's the first error I got with saying the file already exists.
A solution for the first error
Let's get started one by one💪
Take a look at again what the error logs are telling. We see that the file already exists for some reason at this path: /Users/username/.npm-global/bin/tsc
It says to remove it so let's remove it.
$ rm -rf /Users/username/.npm-global/bin/tsc
Let's see how it works by installing TypeScript again. But things are not that simple.
npm ERR! code EEXIST
npm ERR! path /Users/username/.npm-global/bin/tsserver
npm ERR! EEXIST: file already exists
npm ERR! File exists: /Users/username/.npm-global/bin/tsserver
npm ERR! Remove the existing file and try again, or run npm
npm ERR! with --force to overwrite files recklessly.
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/username/.npm/_logs/2022-01-10T22_09_57_095Z-debug.log
Hold on, this time is not throwing an error at tsc
. It's tsserver
this time.
It says to remove it so why not, let's run the command below.
$ rm -rf /Users/username/.npm-global/bin/tsserver
And of course, let's see by installing TypeScript again.
added 1 package, and audited 2 packages in 1s
found 0 vulnerabilities
It succeeded!✨ Let's check it out.
$ tsc -v
Oh no..! Here comes the 2nd error, command not found
.
But good news, it's not that complicated to fix it.
A solution for the second error
Things are quite simple. We just need to make sure 2 things.
Fix the PATH
Reinstall Node.js if necessary
Let's get going by starting to check the directory where npm uses globally. It returns something similar to this path.
$ npm bin -g
/Users/username/.npm-global/bin
Edit at .zshrc
like below. (If you use bash, edit at .bash_profile
.)
$ sudo vi ~/.zshrc
Enter as an insert mode by pressing i
and add this one line in the end.
export PATH=$PATH:`npm bin -g`
Exit by pressing esc
key → :wq
→ enter
key.
Save it by running the command below.
$ source ~/.zshrc
If you don't see any errors or logs, congrats! 🎉 You managed to install TypeScript globally!
However, if you're unlucky or lucky in a way like me, you might see this log.
$ source ~/.zshrc
(not in PATH env variable)
We're almost there...! Jot down a full PATH and run. It should fix the final step.
$ export PATH="/Users/username/.npm-global/bin"
$ souce ~/.zshrc
Let's check out the tsc
command for the last time...!
$ tsc -v
Version 4.5.4
You made it! 👏
Summary
To be honest, it's a super long way and I didn't find a short, clear, and easy answer out there. But that's what made me leave a note for myself in the future and for those of you looking for the solutions.
You don't have to feel reluctant to install TypeScript with npm/yarn/Homebrew.
Hope this article finds you well and helps you!
Ciao👋