Exhibit 5.1 May 8, 2026 Cognex Corporation One Vision Drive Natick, MA 01760-2059 Re: Securities Being Registered under Registration Statement on Form S-8 Ladies and Gentlemen: We have acted as your counsel in connection with your filing of a Registration Statement on Form S-8 (the “Registration Statement”) pursuant to the Securities Act of 1933, as amended (the “Securities Act”), on or about the date hereof relating to an aggregate of 4,600,000 shares (the “Shares”) of Common Stock, par value $0.002 per share (“Common Stock”), of Cognex Corporation, a Massachusetts corporation (the “Company”), that may be issued pursuant to the Company’s 2023 Stock Option and Incentive Plan, as amended (the “Plan”). We have reviewed such documents and made such examination of law as we have deemed appropriate to give the opinion set forth below. We have relied, without independent verification, on certificates of public officials and, as to matters of fact material to the opinion set forth below, on certificates of officers of the Company. For purposes of the opinion set forth below, we have assumed that, at the time Shares are issued, the total number of then unissued Shares, when added to the number of shares of Common Stock issued, subscribed for, or otherwise committed to be issued, does not exceed the number of shares of Common Stock authorized by the Company’s articles of organization. The opinion set forth below is limited to Massachusetts law. Based on the foregoing, we are of the opinion that the Shares have been duly authorized and, when delivered against payment therefor in accordance with the terms of the Plan, will be validly issued, fully paid and nonassessable. This opinion letter and the opinion it contains shall be interpreted in accordance with the Core Opinion Principles as published in 74 Business Lawyer 815 (Summer 2019). We hereby consent to the inclusion of this opinion as Exhibit 5.1 to the Registration Statement. In giving our consent, we do not admit that we are in the category of persons whose consent is required under Section 7 of the Securities Act or the rules and regulations thereunder. Very truly yours, /S/ GOODWIN PROCTER LLP