The home side could have had the game wrapped up by the break after dominating the first half had Barnsley goalkeeper Luke Steele not produced four vital saves, three of them from Zavon Hines.
Jonathan Spector finally beat Steele but the Hammers then stopped playing after the interval and the second half was far more nervy for the Upton Park crowd than it should have been.
Frederic Piquionne's injury-time strike came moments after Barnsley's Adam Hammill saw his deflected shot loop onto the bar and bounce away.
"Make no mistake, we were second best in the first half but the second half was slightly different. It is symptomatic of our season, where we played well for 45 minutes," he said.
"It will be difficult to search for any positives. It could have been comprehensive. The positive was that we only conceded two goals and hit the crossbar."
West ham midfielder Mark Noble said his side's victory was exactly the boost they needed ahead of tomorrow's Carling Cup semi-final against Birmingham.
The Hammers have now won three and drawn two of their last six games and Noble is excited about the opportunity of taking the club back to Wembley for the first time in 30 years.
"A club like this should not be in this position, with the players we have got and the fanbase but we are and we have to dig ourselves out of it once again," said Noble said. "What happened midweek (losing 5-0 at Newcastle) was horrible, it was horrible to be involved with but we needed to put that in the back of our minds. Today could have been a massive banana skin but we came through it.
"We knew it was a terrible performance but there was no point dwelling on it. We needed to win the game today to get a bit of confidence for Tuesday and that is our main focus now.
"A semi-final gives you something to look forward to. We want to beat Birmingham over two legs and be at Wembley."
West Ham United: Green, Faubert, Tomkins, Spector, Reid, Hines (Edgar 90), Noble, Barrera, Boa Morte, Nouble (Piquionne 83), Obinna (Parker 76). Unused substitutes: Boffin, Upson, Sears, Spence.
Barnsley: Steele, Hassell, Shackell, Foster, Hill, Trippier (O'Brien 69), Doyle, Arismendi (Colace 77), Hammill, Lovre (Butterfield 56), O'Connor. Unused substitutes: Preece, Morales Neumann, Gray, Bennett.
Referee: N Swarbrick (Lancashire).
Man of the match: Luke Steele.
Welcome to the Yorkshire Post's exciting match-day service, called Match Centre. Here you will find minute-by-minute updates, stats and team formations. Plus reports from every game in England, from the Premier League down to the Blue Square Premier.
Simply select your game from the pull down menu, then click on the 'i' information button on the right of the fixture and it will give you access to everything you need to know about your team.
So if you can't make it to a game, head for the Match Centre and we will give you the best seat in the house. Click the link below.
For the latest team news, score and commentary from your team check out our new Match Centre
For extensive build-up read the Yorkshire Post on Friday and Saturday, and for full reports and analysis read Sports Monday inside your Yorkshire Post.