In '31, Stalin gave his famous speech about how the Soviet Union was 50-100 years behind the industrialized nations of Europe, and that they only had 5-10 years to make up for this gap, or else, they'd be "crushed." How did Stalin knew that they'd be invaded in 5-10 years? After all, Hitler and the Nazis weren't elected into power for another year or two.
This question presumes that the Nazis and Fascism were seen as the major threat to Communism and the Soviet Union, which is not totally correct. While the rise of Nazi Germany did destabilize Europe and heighten the risk of war, Stalin and the Soviet leadership expected war to break out in Europe in the near future even prior to Hitler's rise to power and had very strong ideological motives for believing so. In the view of Soviet policymakers (and other Marxists), the fundimental struggle was not national but ideological, pitting international Communism (as represented by the USSR and the Comintern) against international Capitalism (the rest of the developed world at the time - namely Europe, the USA, and Japan). Because, in Soviet eyes, the laws of history required a revolutionary conflict which would overthrow the Capitalist system, this was a core facet of Marxist ideology, and not up for debate. Thus the primary threat to the Soviet State always came from the traditional capitalist powers which, through their colonial empires and their control over international commerce, sought to encircle the Soviet Union, deprive it of resources, and ultimately destroy the socialist system in the womb.
The idea of "Capitalist Encirclement," was a core belief of Soviet foreign policy, almost from the inception of the USSR. The Marxist idea of historical materialism necessarily implies a state of conflict between socialist and capitalist systems which will only be resolved through the victory of socialism, and this was the lens through which early Soviet policymakers viewed the world. Trotsky famously described the League of Nations in 1919 as an, "Holy Alliance for the suppression of the workers' revolution." The experience of the Russian Civil War fed these fears, as the US, Britain, France, Japan, and a number of other powers all intervened in support of the White Movements during the Civil War, while the Bolsheviks had no international support. Even after the Bolshevik victory in the Civil War, the Soviet Union remained internationally isolated. While they did sign a trade agreement with Great Britain in 1921 and was recognized by the British in 1924, relations soured in 1927 and did not normalize again until after 1929. The US did not normalize relations with the USSR until 1933, and the USSR was not admitted to the League of Nations until 1934. Relations with most other European powers likewise only normalized in the 1925-1939 period.
Additionally, the sense of isolation and encirclement felt by the Soviets has roots in the historic geopolitical situation of the Russian Empire - the relatively late (post-Napoleonic) recognition of Russia as a Great Power, the struggle for Imperial Russia to access and control markets beyond her borders, the experience of the Crimean War, the earlier struggle for access to warm-water ports, and the general 'backwardness' of Russian society and lack of technological parity with Western Europe all contributed to a sense of Russia being seperate or excluded from the broader European geopolitical system. Thus, the Soviet Union, as the inheritors of the Russian Empire also inherited a legacy of fraught and difficult relations with the West going back to well before Marx.
Early Soviet military policy initially focused on spreading the revolution beyond Soviet borders - as exemplified by the Polish-Soviet War and their hopes of promoting a revolution in Germany. This policy changed after Stalin took power, shifting to a less expansionist military focused on the defense of the Soviet Union (see the Trotskist-Stalinist disagreements over international revolution vs. "socialism in one country.") By the time of your question, the Stalinist faction had won and Soviet policy was focused on rapid industrialization in order to support the modernization and expansion of the Red Army. It may thus be informative to view some of Stalin's statements as justification for this ambitious industrialization and military expansion policy. However, this should not downplay the real sense of animosity and alienation felt by Stalin and other Soviet leaders towards the West, nor their expectations that war was coming between the USSR and the Capitalist Powers, even prior to the rise of Nazi Germany and the Anti-Comintern Pact.
"The Sources of Soviet Conduct," by George F Kennan. Published in Foreign Affairs, July 1947.
"Soviet Foreign Policy, Volume I, 1917-1945," by I. N. Zemskov, I. F. Ivashin, V. L. Israelyan, et al. Published 1980.
"Revolution and Integration in Soviet International Diplomacy, 1917–1991," by Steven White and Steven Revell. Published in Review of International Studies, 1999.
"Political Report of the Central Comittee, December 3, to the Fifteenth Congress of the C.P.S.U.," by J. V. Stalin. Published in Works, Volume 10, August-December 1927.
EDITS: for clarity because I didn't proofread this.