Running nodejs server fails - Syntax Error: word unexpected (expecting ")")

Hello guys,

I’m currently trying to build a snap targeting the Raspberry Pi 4 Model B running Ubuntu Core 18 arm64. I first want it to run on my local machine (Ubuntu 16.04). Basically I want to set up a nodejs server, so I am using the nodejs plugin

My first snapcraft.yaml looks like that:

name: printer-server-iot
base: core18
version: '0.1' # just for humans, typically '1.2+git' or '1.3.2'
summary: printer server with iot compabilities
description: |
  local printer server used for controlling thermal printers

grade: devel
confinement: devmode

parts:
  printer-server-iot:
    plugin: nodejs
    nodejs-version: 12.16.2
    nodejs-package-manager: npm
    source: .

# check server status by: systemctl status snap.printer-server-iot.printer-server-iot.service
apps:
  printer-server-iot:
    command: printer-server-iot
    daemon: simple

I’m building the image by “snapcraft --debug” which works fine. Afterwards I install the snap by “snap install printer-server-iot_0.1_amd64.snap --devmode”.
Now when I check the service status of the server by “systemctl status snap.[…]”, I can see that the server has failed to start and is not running.

I checked the logs of the snap by “snap logs printer-server-iot”. And I get the following logs:

2020-11-30T13:59:28Z printer-server-iot.printer-server-iot[16150]: /snap/printer-server-iot/x1/bin/printer-server-iot: 1: /snap/printer-server-iot/x1/bin/printer-server-iot: Syntax error: word unexpected (expecting “)”)
2020-11-30T13:59:28Z systemd[1]: snap.printer-server-iot.printer-server-iot.service: Main process exited, code=exited, status=2/INVALIDARGUMENT
2020-11-30T13:59:28Z systemd[1]: snap.printer-server-iot.printer-server-iot.service: Unit entered failed state.
2020-11-30T13:59:28Z systemd[1]: snap.printer-server-iot.printer-server-iot.service: Failed with result ‘exit-code’.
2020-11-30T13:59:29Z systemd[1]: snap.printer-server-iot.printer-server-iot.service: Service hold-off time over, scheduling restart.
2020-11-30T13:59:29Z systemd[1]: Stopped Service for snap application printer-server-iot.printer-server-iot.
2020-11-30T13:59:29Z systemd[1]: snap.printer-server-iot.printer-server-iot.service: Start request repeated too quickly.
2020-11-30T13:59:29Z systemd[1]: Failed to start Service for snap application printer-server-iot.printer-server-iot.
2020-11-30T13:59:29Z systemd[1]: snap.printer-server-iot.printer-server-iot.service: Unit entered failed state.
2020-11-30T13:59:29Z systemd[1]: snap.printer-server-iot.printer-server-iot.service: Failed with result ‘start-limit-hit’.

Which points me to the error - Syntax error: word unexpected (expecting “)”)

I investigated some time to find out about the root of this error and the resources often pointing me to some cross-compilation issues and hardware architecture mix ups.

Previously I have built some other example snaps based on the go plugin instead of nodejs. The examples before worked fine on my machine. But this time It doesn’t.

I’m pretty new to snapcraft and building images I hope you can help me.

Thank you guys.

UPDATE:

I have also tried to run it on Ubuntu 18.04 and I get the same Error.

I suspect that your JS program is being interpreted as a shell script. Maybe adding #!/usr/bin/env node as the first line of the script will help?

Thank you, @jamesh. That seemed to solve my problem. I have created a file in a new bin directory adding #!/usr/bin/env node as the first line and requiring my NodeJS build below (So I created a kind of wrapper file).

I still would suggest one thing here. Since I have built an executable js file beforehand by webpack, I actually don’t need the package.json mechanisms of snapcraft. It would be nice to just define the plugin and define the source js file for this use case. I think a lot of people would love to have that option.

The alternative would be to put the invocation of node in your snapcraft.yaml file. Probably changing your app definition to use something like:

command: bin/node $SNAP/bin/printer-server-iot

I don’t think this is a webpack vs. no webpack issue, but I don’t do a lot of JS programming.