where did I lose the negative sign when spring is in compression in a mass-spring problem? Most books just tells you that spring force is $-kx$ since it opposes motion and to just write $mx''=-kx$ regardless if the spring is in tension of compression.
But when I try to derive this using $F=mx''$ and using free body diagram, and put the mass to the right of the equilibrium position, I get the correct equation of motion, but when the mass is to the left, I do not. 
Please tell me what I am doing wrong (and do not just say to use negative sign since it is a restoring force). I need to find the EQM myself from free body diagram.
When the mass to the right we have
                     x
   |             |<----->
   |             |       k x (spring force since in tension
   |-------------|------<----o mass
   |             |
   |            x=0

So applying $F=mx''$ on the above gives $mx''=-kx$ which is the correct EQM. I used minus sign here since the direction of the force vector $kx$ is negative and not because some book said to use negative sign all the time.  
Now lets look at it when the mass is to the left and the spring is in compression
              x
   |      <------>
   |             |      
   |---->o       |
   |   kx       x=0
   |

So applying $F=mx''$ gives $mx''=kx$ which is wrong. 
The force $kx$ is now in the positive direction. So how to make $kx$ become negative?  $x$ is a extension amount. So it is always a positive number as far as finding the force in the spring is concerned. $x$ as a coordinate is negative, yes, but for Hooke's law, force in spring is proportional to extension. Extension is always positive regadless of which way it is.
So when in compression, we have a force pointing in the positive direction and has an amount of $|kx|$. But I need to obtain $mx''=-kx$ even when the spring is in compression. I think my problem is in the $mx''$ term and not in the $kx$ term.
What Am I missing? Where did I go wrong? How to get $mx''=-kx$ when mass to the left using just free body diagram?
adding
This is below is attempt to get the same EQM using D`Alembert's principle. I do not know it well yet, but actually now I get the correct EQM when the spring is in tension or compression using this. 
One is supposed to write $F-(mx'')=0$ where now  $mx''$ is the so called fictitious inertial force that is always in the opposite direction to the resulting applied forces and acts on the same line. 
So using this: When the mass is to the right of the $x=0$ we get $F-(mx'')=0$ or $-kx-(mx'')=0$  or $kx+mx''=0$ so this works ok.
Now lets try it when the mass to the left, we have $F-(-mx'')=0$ where I added a negative sign for $-mx''$ since now it is pointing to the left, i.e. negative. This is because now the force in spring is pointing to the right.
So now we have $kx-(-mx'')=0$  or $kx+mx''=0$ which is the correct EQM !
Is the above correct way of using D`Alembert's principle on this problem? 
 A: I am just repeating what everyone has said here, but hopefully this will make it clearer.
What you are doing is conflating $x$ as a coordinate and as a distance. When we use Newton's laws $F = m \ddot{x}$, $x$ is a coordinate.
Let's set up the scenario more carefully than you have done. Let's have a coordinate system where rightwards is positive. Say we have a spring of unstretched length $l$ (this is a distance, not a coordinate). Fix one end of the spring be at coordinate $0$, and let the free end be at coordinate $l$ initially.
Now we consider perturbations about this equilibrium configuration. Define $x$ to be the displacement of the free end from coordinate $l$, so that the coordinate of the free end is now given by $y = l + x$ (it is a function of time). Note that $x$ is not the extension amount. The extension amount is instead $|x|$. 
Now we can do what you did, consider the two cases. When the spring is extended, that is, when $x > 0$, we have from Newton's 2nd law:
\begin{align}
m \ddot{y} = -k|x|.
\end{align}
Ok let's pause to see what I've written here. the LHS is the 2nd time derivative of the coordinate of the free end of the spring (with a mass attached to it). The right hand side is the sum of forces, which by Hooke's law has magnitude $k|x|$ and I have thrown in the minus sign because it points to the left.
But note that $\ddot{y} = \ddot{(l + x)} = \ddot{x}$ since $l$ is a constant. Also, since $x > 0$, $|x| = x$. So we get
\begin{align}
m\ddot{x} = -kx.
\end{align}
When the spring is compressed, $x < 0$. So 
\begin{align}
m\ddot{y} = k|x|.
\end{align}
the LHS is exactly the same as before, and the RHS has now a positive sign because the force points to the right. But $x < 0$ means that $|x| = -x$, so we get
\begin{align}
m\ddot{x} = -kx,
\end{align}
exactly the same as before.
So there is no trouble.
Now you might protest and say, hey I want to define $x$ to be the distance of the free end from the equilibrium length, so $x \geq 0$, instead of as the displacement. Then what changes is this:
the coordinate is now a piecewise function. If the spring is stretched, $y = l + x$; if it is compressed, $y = l-x$.
For the stretched case, writing Newton's law gives
\begin{align}
m \ddot{y} = m\ddot{x} = -kx.
\end{align}
For the compressed case, 
\begin{align}
m \ddot{y} = -m \ddot{x} = kx \implies m\ddot{x} = -kx.
\end{align}
Once again, we get back the same equations, and there is no problem.
A: The situations are completely symmetric. When the spring is stretched, the "excess position" $x$ is positive and the force $-kx$ is negative, trying to return the spring to the equilibrium length (force pushes it to the left). When the spring is compressed, the "excess position" $x$ is negative and the force $-kx$ is positive (two signs, one from the explicit $-$ and one from $x\lt 0$, cancel), trying to return the spring to the equilibrium again (force is pushing to the right now).
Regardless of the origin and magnitude of the forces, it's always true that $F=ma=m\ddot x$.
A: I don't see the problem. The $x$ refers to a coordinate here. When you have compressed the spring, your $x$ is negative, so your $-kx$ is positive as it should be. If you want your $x$ to instead refer to the positive magnitude of extension then you can't get a nice equation like $F = -kx$, you will have to make a piece-wise function to describe the force in terms of this $x$.
And to be clear, Hooke's law is this: For a spring, the magnitude of the force is proportional to the displacement from the equilibrium position and it is always opposite in direction to the displacement. 
You seem to have made an argument using this definition, but I don't really understand it. If you want your $x$ to instead refer to the 'extension amount' which is always positive, you'll have to make a piece-wise definition for the force as I already said.
