You can replace the crankshaft in some engines without removing the engine from the vehicle.
But you have to take the transmission lose from the engine and slide the transmission back away from the engine to detach the crankshaft from the transmission.
It's usually much easier to just pull the entire motor out and then flip the engine over to access the crankshaft to repair it easier.
I've had to replace a crankshaft before on an old truck when I didn't have a way to remove the engine and it was a pain and I would not recommend doing it that way.
I find it much easier to use an engine hoist and lift the engine out and use an engine stand to work on the engine that way.
Working under a car is less fun than just standing up or sitting down on a chair and working on the engine from an engine stand.
The crankshaft can be pretty heavy to lift up to the engine so I would just pull the motor out and fix it that way.
But on some vehicles you certainly can replace a crankshaft with the engine in the vehicle but it's actually more work than just pulling the motor out and doing the job that way.