vuestorefront / vue-storefront

Alokai is a Frontend as a Service solution that simplifies composable commerce. It connects all the technologies needed to build and deploy fast & scalable ecommerce frontends. It guides merchants to deliver exceptional customer experiences quickly and easily.
https://www.alokai.com
MIT License
10.65k stars 2.09k forks source link

SyntaxError: Unexpected Token { on v1.11 after yarn start #3842

Closed haelbichalex closed 4 years ago

haelbichalex commented 5 years ago

Current behavior

When running yarn start I see the following error in pm2 logs for server and it fails to start:

40|server  | /core/scripts/server.ts:1
40|server  | import { serverHooksExecutors } from '@vue-storefront/core/server/hooks'
40|server  |        ^
40|server  |
40|server  | SyntaxError: Unexpected token {
40|server  |     at Module._compile (internal/modules/cjs/loader.js:723:23)
40|server  |     at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
40|server  |     at Module.load (internal/modules/cjs/loader.js:653:32)
40|server  |     at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
40|server  |     at Function.Module._load (internal/modules/cjs/loader.js:585:3)
40|server  |     at Function._load (/usr/lib/node_modules/pm2/node_modules/@pm2/io/build/main/metrics/httpMetrics.js:172:43)
40|server  |     at /usr/lib/node_modules/pm2/lib/ProcessContainer.js:295:23
40|server  |     at /usr/lib/node_modules/pm2/node_modules/async/internal/once.js:12:16
40|server  |     at next (/usr/lib/node_modules/pm2/node_modules/async/waterfall.js:21:29)
40|server  |     at /usr/lib/node_modules/pm2/node_modules/async/internal/onlyOnce.js:12:16

image

Expected behavior

yarn start should work without errors

Steps to reproduce the issue

Repository

Can you handle fixing this bug by yourself?

Which Release Cycle state this refers to? Info for developer.

Pick one option.

Environment details

Additional information

andrzejewsky commented 5 years ago

@haelbichalex I've just tested that, everything works fine. Could you maybe add some details about that?

Screenshot 2019-11-26 at 17 03 33
haelbichalex commented 5 years ago

This happens for me on our staging environment, here's the yarn pm2 report:

===============================================================================
--- PM2 REPORT (Tue Nov 26 2019 17:10:21 GMT+0100 (Central European Standard Time))
===============================================================================
--- Daemon -------------------------------------------------
pm2d version         : 3.5.0
node version         : 10.17.0
node path            : /usr/bin/pm2
argv                 : /usr/bin/node,/usr/lib/node_modules/pm2/lib/Daemon.js
argv0                : node
user                 : web-user
uid                  : 1000
gid                  : 1000
uptime               : 451min
===============================================================================
--- CLI ----------------------------------------------------
local pm2            : 2.10.4
node version         : 10.17.0
node path            : /usr/bin/yarn
argv                 : /usr/bin/node,/var/www/share/guf-frontend/releases/231/node_modules/.bin/pm2,report
argv0                : /usr/bin/node
user                 : web-user
uid                  : 1000
gid                  : 1000
===============================================================================
--- System info --------------------------------------------
arch                 : x64
platform             : linux
type                 : Linux
cpus                 : Intel(R) Xeon(R) Gold 6132 CPU @ 2.60GHz
cpus nb              : 2
freemem              : 2516652032
totalmem             : 7516192768
home                 : /var/www/share
===============================================================================

Also, I got it to work with the follwing ecosystem.json

{
  "apps": [
    {
      "name": "server",
      "max_memory_restart": "1G",
      "instances": "4",
      "exec_mode": "cluster",
      "env": {
        "TS_NODE_PROJECT": "tsconfig-build.json",
        "NODE_ENV": "production"
      },
      "script": "./node_modules/.bin/ts-node",
      "args": "-P ./tsconfig-build.json ./core/scripts/server.ts",
      "node_args": "--max_old_space_size=1024",
      "log_date_format": "YYYY-MM-DD HH:mm:ss",
      "ignore_watch": [
        "core/build/config.json",
        "node_modules"
      ]
    }
  ]
}

Also I found something interesting in pm2 logs, but I couldn't find out why it's happening, sind I'm able to run yarn ts-node:

Failed to load Typescript interpreter: Cannot find module 'ts-node/register'
ghost commented 5 years ago

Hi, I have the same problem, today I went crazy during production deployment and after several hours I discovered the problem is base code from release branch v1.11. When I switch to master, everything works as expected, in our dev branch (based on release/v1.11) pm2 processes do not start at all with the following error:

[2019-11-26 16:08:15] PM2 log: Script C:\Users\libor.pansky\source\repos\vue-storefront\core\scripts\server.ts had too many unstable restarts (16). Stopped. "errored"
[2019-11-26 16:08:15] PM2 log: App name:server id:3 online
undefined:1
undefined
^

SyntaxError: Unexpected token u in JSON at position 0
    at JSON.parse (<anonymous>)
    at Object.<anonymous> (C:\Users\libor.pansky\source\repos\vue-storefront\node_modules\pm2\lib\ProcessContainer.js:17:20)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:743:3)

(there's probably missing pm2 environment variable pm2_env)

PM2 module error is the same as @haelbichalex 's:

SyntaxError: Unexpected token {
    at new Script (vm.js:79:7)
    at createScript (vm.js:251:10)
    at Object.runInThisContext (vm.js:303:10)
    at Module._compile (internal/modules/cjs/loader.js:657:28)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at C:\Users\libor.pansky\AppData\Roaming\npm\node_modules\pm2\lib\ProcessContainer.js:297:23
    at wrapper (C:\Users\libor.pansky\AppData\Roaming\npm\node_modules\pm2\node_modules\async\internal\once.js:12:16)
C:\Projects\CDSHotels-external\vue-storefront\core\scripts\server.ts:1
(function (exports, require, module, __filename, __dirname) { import { serverHooksExecutors } from '@vue-storefront/core/server/hooks'

BTW: happened on both Debian x64 and Windows 10 machines

andrzejewsky commented 5 years ago

Ok, thanks for reporting we'll try figure it out as soon as possible.

andrzejewsky commented 5 years ago

@cdshotels-liborpansky @haelbichalex are you guys on our community slack? I'd like to ask you few questions, that would be helpful for me.

haelbichalex commented 5 years ago

@andrzejewsky sure 😉 PM'ed you

Fifciu commented 5 years ago

I had a similar problem with the new API yesterday. Files while building process are transformed to the different compatibility mode (commonjs and stuff like that). I am not sure if the same happens in the PWA. But it might be that.

Sooo... the reason of error could be import from dist to some file which is outside of the dist. For example, in src

gcampedelli commented 4 years ago

Hello, above fix didn't work for me in the same error. I was reading this and I tried to run this command : "pm2 start /path-to/vuestorefront/vue-storefront/core/scripts/server.ts" And then this error showed up [PM2][ERROR] Interpreter /usr/lib/node_modules/pm2/node_modules/.bin/ts-node is NOT AVAILABLE in PATH. (type 'which /usr/lib/node_modules/pm2/node_modules/.bin/ts-node' to double check.)

So I went to /usr/lib/node_modules/pm2/node_modules/.bin/ts-node and I installed typescript from there And this error went away, but I'm still getting 502 in server