As the scale of global trade gets bigger, many small and midsize U.S. ports, such as the Port of Portland, face the prospect of falling off the map entirely.

Barges loaded with Idaho-grown peas and lentils until this spring regularly chugged downriver to Portland’s port, the first leg in a journey that would end in supermarkets and restaurants across Asia and Europe.

But now Idaho’s farmers—along with Oregon’s grass seed growers as well as manufacturers and other exporters across the Pacific Northwest—need a new route to the global market.

The last major container shipping line ended its Portland run in March, leaving the city without regular ocean-bound container service for the first time in four decades. Exporters who relied on the port say their transportation costs soared overnight.

Fearing a similar slide into irrelevance, port operators from Newark, N.J., to Long Beach, Calif., are spending billions of dollars to dredge harbors, raise bridges and build larger terminals to accommodate megaships.

Portland’s woes have created hardships for farmers and other exporters who say they are paying much more to send their crops 150 miles north to the Seattle area. Portland officials estimate that an additional 1,700 heavy trucks are carrying cargo north on the main regional freeways each week, causing traffic snarls.

“It’s going to be a really tough road,” said Andrew Fontaine, general manager at Spokane Seed Co., which used to send its peas, lentils and chickpeas via barge from Lewiston, Idaho, to Portland. His company now pays 30% more to truck its goods to the ports of Seattle and Tacoma.

In 2014, the Port of Portland ranked No. 21 among U.S. ports by full containers, according to maritime research firm PIERS. It moved about 130,000 20-foot container equivalent units, or TEUs, full of goods through its port. Los Angeles, the nation’s largest port for container cargo, moved 5.9 million full TEUs last year.

Part of the problem for smaller ports stems from shipping lines’ desire to bulk up by ordering vessels that carry twice as much cargo as the largest ships at sea 10 years ago. For them, Portland’s harbor is too far inland, too shallow and plagued with delays.

Along with Portland, ports in cities including Seattle and Tacoma, Wash., Jacksonville, Fla., and Baltimore are fighting to keep ships from shifting routes to bigger rivals.

Top Logistics News Get the latest logistics and supply chain news and analysis via an email newsletter. Sign up here.

Importers and exporters could lose direct access to global trade lanes, raising their costs and hampering their ability to connect to foreign markets.

Ports that can’t keep up face bleak prospects, experts say. The trend toward bigger ships and bigger ports will accelerate once the Panama Canal finishes its expansion next year, making it easier for large vessels to navigate between the Atlantic and Pacific oceans.

Shipping lines are going through tough economic times as capacity from these bigger ships outstrips demand. They are trying to run the vessels at top efficiency in an attempt to crowd out smaller rivals while they wait for economic growth to create more container traffic and justify their investment in larger vessels.

“The reality is those [smaller] ports are not going to be able to sustain themselves and be competitive handling the deep-sea vessels that will be in prominent use on major East-West trade routes,” said Paul Bingham, an economist with the Economic Development Research Group Inc., a consulting firm.

Portland’s problems are in some ways typical of many smaller ports that have fared poorly in recent years. But it also faces unique challenges.

Importers and exporters in regions with smaller ports like Portland face the prospect of losing direct access to global trade lanes if shipping lines move to bigger rivals. Photo: Leah Nash for The Wall Street Journal

Reaching the city’s harbor requires a winding 100-mile journey up the Columbia River, adding hours to a voyage. The harbor is only deep enough to handle ships that carry one-third the load of the largest vessels, or about 6,000-6,500 container units, reducing the economies of scale that lure shipping lines and large exporters. A labor dispute between dockworkers and the container terminal operator has caused delays in handling goods for much of the past two years.

In February, South Korea’s Hanjin Heavy Industries & Construction Co. Ltd., which moved the majority of ocean-bound containers through Portland’s Terminal 6, halted service. The other major container-shipping line in Portland, Germany’s Hapag-Lloyd AG, said in early April that it would do the same. After Hapag-Lloyd made its final call, no container ships stopped in Portland until late July, when Westwood Shipping Lines Inc., a much smaller operator, sent a vessel.

Hapag-Lloyd has no plans to return to Portland. A spokesman cites “a need for efficiency and a competitive product,” as its reason for pulling out. Hanjin could come back if the port sees fewer delays, said Mike Radak, the company’s senior vice president for operations and sales in North America. The company is loading cargo it carried on its Portland route onto a larger ship at the port in Seattle-Tacoma.

Some ports are taking steps to avoid Portland’s fate.

Over the next 10 years, the South Carolina Ports Authority and the State of South Carolina plan to spend a combined $2 billion at the Port of Charleston to build a container terminal, deepen its channel and expand road and rail access. The Port Authority of New York and New Jersey is raising the Bayonne Bridge connecting the states to allow larger ships to pass underneath. The goal in both cases: lure giant container ships.

Portland’s container traffic, largely representing exports, peaked in 2003 at 340,000 20-foot equivalent units, a standard measure for container cargo. Above, Portland’s harbor in 2012. Photo: Rick Bowmer/Associated Press

Portland hasn’t ground to a complete halt. The port’s other marine terminals handle cargo that isn’t shipped in containers, including cars and bulk commodities. The container terminal operator, Philippines-based International Container Terminal Services Inc., is trying to bring in new business, said Elvis Ganda, the company’s Oregon chief executive.

ICTSI, which operates 29 terminals in more than 20 countries, is just four years into a 25-year agreement with the port, paying $4.8 million annually to rent the terminal facility.

Analysts say Portland and other smaller ports can thrive by focusing on niche routes and finding customers in new parts of the world.

“A lot of ports will be able to still play the game,” said John Martin, a maritime economist at Martin Associates in Lancaster, Pa.

For now, farmers like Shelly Boshart Davis, one of many grass seed growers in the Willamette Valley just south of Portland, face an uncertain future. She has been sending exports for her company BOSSCO Trading LLC—about 55 to 60 containers a week—to the Seattle-Tacoma port at an extra cost of $700 to $1,000 per container, roughly triple to what she was paying before.

The busy harvest season, which started up in late June, will be a learning experience, Ms. Boshart Davis said.

“In the next couple months, we’ll find out what the new normal is going to look like,” she said.

Write to Erica E. Phillips at erica.phillips@wsj.com