Core–shell structured Si/C nanocomposites with different nitrogen contents are prepared by in situ polymerization of aniline in the suspension of silicon nanoparticles followed by carbonization of Si/polyaniline (PANI) nanocomposites at different temperatures. The nitrogen contents of Si/C nanocomposites decrease gradually with increasing carbonization temperatures. The effect of nitrogen contents on the electrochemical performance of Si/C nanocomposites as anode materials for lithium ion batteries is investigated. It is found that the Si/C nanocomposites with 4.75wt.% nitrogen exhibit the high specific capacity of 795mAhg−1 after 50 cycles at a current density of 100mAg−1 and excellent cycling stability. The appropriate nitrogen in Si/C nanocomposites plays a beneficial role in the improvement of electrochemical performance. The nitrogen in Si/C nanocomposites increases the reversible capacity, which may be due to the formation of vacancies and dangling bonds around the nitrogen sites.