dwgebler / node-express-example

Sample Node/Express tutorial app
0 stars 0 forks source link

SyntaxError: Unexpected number thrown by node-server container triggered by wait.sh #1

Open ChristianOConnor opened 1 year ago

ChristianOConnor commented 1 year ago

When I run the command docker compose up -d in the same directory as the docker-compose.yml file, I get this in the node-server logs:

SyntaxError: Unexpected number
    at wrapSafe (internal/modules/cjs/loader.js:1001:16)
    at Module._compile (internal/modules/cjs/loader.js:1049:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
    at Module.load (internal/modules/cjs/loader.js:950:32)
    at Function.Module._load (internal/modules/cjs/loader.js:790:12)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:75:12)
    at internal/main/run_main_module.js:17:47
/var/app/wait.sh:4
shift 2

The wait.sh file where the error is occuring looks like this:

#!/bin/sh
host="$1"
port="$2"
shift 2
cmd="$@"
until nc -z "$host" "$port"; do
  >&2 echo "Mongo is unavailable - sleeping"
  sleep 1
done
>&2 echo "Mongo started"
exec $cmd

So why is "shift 2" causing this SyntaxError: Unexpected number error? How do I fix this?

dwgebler commented 1 year ago

Hi @ChristianOConnor the error you're seeing is actually in the JavaScript execution, not the shift shell command. So in the tutorial on my blog, I didn't completely Dockerize this example; it assumes you're running Node 14 both in the container and on your host. This was just to simplify the instructions when I wrote it. I suspect what's happened here is you have a different version of Node on your host. I'd suggest downloading and installing Node Version Manager (nvm) and running nvm use 14 on your host machine before you run

npm init -y
npm install express mongodb

as per the tutorial steps. Hope this helps!