Formaldehyde (FA) is an active carbonyl structure that is widely used in people's daily life, such as preservatives and fixatives. Furthermore, formaldehyde is highly toxic and has been listed as one of the three major indoor pollutants. Therefore, the development of highly sensitive and selective fluorescent probes is of great biological importance. In this article, probe X is designed to identify formaldehyde by a light-induced electron transfer (PET) process between the naphthalimide derivative and the hydrazine group based on hydrazone formation. Moreover, the probe has good optical response and excellent linearity over a wide range of formaldehyde concentrations, ensuring that formaldehyde concentration values in real complex food samples can be quantitatively recorded. In addition, the probe can monitor the changes of formaldehyde levels in living cells with good biocompatibility and image resolution.