Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SyntaxError: Invalid or unexpected token #41

Closed
WilianZilv opened this issue Dec 13, 2019 · 38 comments
Closed

SyntaxError: Invalid or unexpected token #41

WilianZilv opened this issue Dec 13, 2019 · 38 comments

Comments

@WilianZilv
Copy link

Sending messages to worker:

(node:30) UnhandledPromiseRejectionWarning: SyntaxError: Invalid or unexpected token
at runMicrotasks (<anonymous>)
at processTicksAndRejections (internal/process/task_queues.js:93:5)
(node:30) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)

Express random error when i enter a route that doesn't exists

SyntaxError: Invalid or unexpected token
    at Layer.handle [as handle_request] (/sistema/server/node_modules/express/lib/router/layer.js:95:5)
    at next (/sistema/server/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/sistema/server/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/sistema/server/node_modules/express/lib/router/layer.js:95:5)
    at /sistema/server/node_modules/express/lib/router/index.js:281:22
    at param (/sistema/server/node_modules/express/lib/router/index.js:354:14)
    at param (/sistema/server/node_modules/express/lib/router/index.js:365:14)
    at Function.process_params (/sistema/server/node_modules/express/lib/router/index.js:410:3)
    at next (/sistema/server/node_modules/express/lib/router/index.js:275:10)
    at SendStream.error (/sistema/server/node_modules/serve-static/index.js:121:7)
    at SendStream.emit (events.js:210:5)
    at SendStream.error (/sistema/server/node_modules/send/index.js:270:17)
    at SendStream.onStatError (/sistema/server/node_modules/send/index.js:421:12)
    at next (/sistema/server/node_modules/send/index.js:735:16)
    at onstat (/sistema/server/node_modules/send/index.js:724:14)
    at FSReqCallback.oncomplete (fs.js:158:21)

This is one of the many errors i get when i run my project compiled with bytenode. Any help? My project is stable with the source code. But i need to protect it.

@WilianZilv
Copy link
Author

Sometimes those errors don't occur, it's just random

@WilianZilv
Copy link
Author

Do i need to require bytenode in every file?

@OsamaAbbas
Copy link
Collaborator

Could you please provide an example that reproduce the issue?

Do i need to require bytenode in every file?

In general, no. But as your app is using workers I need to see an example.

@WilianZilv
Copy link
Author

WilianZilv commented Dec 14, 2019

my express setup is basically it:

const express = require("express");
const app = express();
const http = require("http").createServer(app);
app.use(express.json());

app.use("/", express.static("public"));
app.get("*", (req, res) => res.redirect("/"));

http.listen(80);

And there is some routes for my API. Using express Router.

For my workers, i use this little script i wrote to help me both on development and production (this one i leave as a .js because i need some .js for the workerPath:

require("bytenode");
const {
    isMainThread,
    parentPort,
    workerData,
    Worker
} = require("worker_threads");

if (isMainThread) {
    module.exports = (initiator, workerPath, workerData) => {
        const worker = new Worker(__filename, {
            workerData: { workerPath, workerData }
        });
        worker.on("exit", console.log);
        initiator(worker);
    };
} else {
    const { workerPath } = workerData;
    try {
        require(workerPath)(parentPort, workerData.workerData);
    } catch (err) {
        require(workerPath + ".jsc")(parentPort, workerData.workerData);
    }
}

I use socket.io too, and i got the same message on the errors "Invalid or unexpected token".

Notice that i said the program starts normal and after running for one minute or so, those errors start to appear until the program crashes. And sometimes those errors appears just when i start and access some route or my worker/main thread exchange data.

(Sorry if i wrote something wrong, my english is not 100%)

@OsamaAbbas
Copy link
Collaborator

Your example isn't clear. Please provide a complete workable example, with filenames and the compiling process itself.

@WilianZilv
Copy link
Author

I don't have much time today. But do you have any idea of what is happening? Or node version recomendation. I'm currently using v12.13.0

@OsamaAbbas
Copy link
Collaborator

Take your time.

I don't know what or where does your issue come from without a complete example that reproduce the error.

@WilianZilv
Copy link
Author

WilianZilv commented Dec 14, 2019

But basically i compile every .js of my project using bytenode.compileFile, including the main index.js and except that one i use to start a worker.
Then i start my application with a script that contains the following code:
require("bytenode"); require("index.jsc");

@OsamaAbbas
Copy link
Collaborator

OsamaAbbas commented Dec 14, 2019

It's not clear how your worker file will call the express server.

Without a complete example that I can run with a simple npm start, there is a little that I can do.

Just make a very minimal example that contains pcakcage.json, server.js and worker.js and reflects accurately your issue.

@WilianZilv
Copy link
Author

Oh, my worker is just a part of my project, they are are not related, they just happen to run in the same project

@WilianZilv
Copy link
Author

But after tomorrow i'll test if a simple express server throw those errors, if it does, i'm missing something

@WilianZilv
Copy link
Author

I read the README, and i missed two things:

  1. I'm not compiling my modules with compileAsModule: true (100% of my files are modules, except the main index.js)

  2. I'm not running or setting the flag --no-lazy

I'll make those changes tomorrow and see if makes difference.

@IBMRob
Copy link

IBMRob commented Dec 19, 2019

We don't have a simple re-create but we are also hitting this error. We are also using an express app.
As we were trying to debug it, we were not compile certain files of our app and it would start working but then once we merged the fix into master and rebuilt it then started failing. All a bit random.

@OsamaAbbas
Copy link
Collaborator

As I mentioned above, without a concrete example there is a little that we can do.

You may email me with more details on your source code and the issue that you have if you can't disclose such details here.

@WilianZilv
Copy link
Author

@IBMRob are you using docker to run your application? because i am. It seems it is not a normal problem

@IBMRob
Copy link

IBMRob commented Dec 19, 2019

@WilianZilv - Yes - we only see the problem when running it in a docker container/openshift. When we run it locally it all works fine.

@OsamaAbbas
Copy link
Collaborator

Ok. That's a good starting point. I will look into it.

@OsamaAbbas
Copy link
Collaborator

Are you sure that Node.js version and arch is the same locally and in docker?

@WilianZilv
Copy link
Author

In my case i share my project folder to a container that uses the same image as the other container uses to run the application and then i compile the scripts there. So the node version and system match

@IBMRob
Copy link

IBMRob commented Dec 20, 2019

Yup We are using the same docker image for both compiling and running - registry.access.redhat.com/ubi8/nodejs-12

@WilianZilv
Copy link
Author

I coudn't reproduce my issue... It's so weird.
I have some routes in my API that returns a JSON, when i make a request, it completes with no problem at the same time this SyntaxError log appears in the terminal

@WilianZilv
Copy link
Author

I have discovered something:
Whenever i try to request something that doesn't exists, this SyntaxError log shows up!

Even tho i'm using this piece of code to redirect to /
app.use((req, res) => res.redirect("/"));

image

@yingjun
Copy link

yingjun commented Dec 27, 2019

same problem here, node12 + babel+es6 syntax.

PS: NO Problem when using node10 + babel+es6 syntax, I can only reproduce this problem with node12

SyntaxError: Invalid or unexpected token at Layer.handle [as handle_request] (project/node_modules/express/lib/router/layer.js:95:5) at next (project/node_modules/express/lib/router/route.js:137:13) at ge (evalmachine.<anonymous>:1:14901) at Layer.handle [as handle_request] (project/node_modules/express/lib/router/layer.js:95:5) at next (project/node_modules/express/lib/router/route.js:137:13) at Route.dispatch (project/node_modules/express/lib/router/route.js:112:3) at Layer.handle [as handle_request] (project/node_modules/express/lib/router/layer.js:95:5) at project/node_modules/express/lib/router/index.js:281:22 at Function.process_params (project/node_modules/express/lib/router/index.js:335:12) at next (project/node_modules/express/lib/router/index.js:275:10) at Function.handle (project/node_modules/express/lib/router/index.js:174:3) at router (project/node_modules/express/lib/router/index.js:47:12) at Layer.handle [as handle_request] (project/node_modules/express/lib/router/layer.js:95:5) at trim_prefix (project/node_modules/express/lib/router/index.js:317:13) at project/node_modules/express/lib/router/index.js:284:7 at Function.process_params (project/node_modules/express/lib/router/index.js:335:12) at next (project/node_modules/express/lib/router/index.js:275:10) at serveStatic (project/node_modules/serve-static/index.js:75:16) at Layer.handle [as handle_request] (project/node_modules/express/lib/router/layer.js:95:5) at trim_prefix (project/node_modules/express/lib/router/index.js:317:13) at project/node_modules/express/lib/router/index.js:284:7 at Function.process_params (project/node_modules/express/lib/router/index.js:335:12) at next (project/node_modules/express/lib/router/index.js:275:10) at urlencodedParser (project/node_modules/body-parser/lib/types/urlencoded.js:82:7) at Layer.handle [as handle_request] (project/node_modules/express/lib/router/layer.js:95:5) at trim_prefix (project/node_modules/express/lib/router/index.js:317:13) at project/node_modules/express/lib/router/index.js:284:7 at Function.process_params (project/node_modules/express/lib/router/index.js:335:12) at next (project/node_modules/express/lib/router/index.js:275:10) at project/node_modules/body-parser/lib/read.js:130:5 at invokeCallback (project/node_modules/raw-body/index.js:224:16) at done (project/node_modules/raw-body/index.js:213:7) at IncomingMessage.onEnd (project/node_modules/raw-body/index.js:273:7) at IncomingMessage.emit (events.js:215:7)

@OsamaAbbas
Copy link
Collaborator

@yingjun Can you email me with a minimal reproducible example?

@yingjun
Copy link

yingjun commented Dec 30, 2019

@OsamaAbbas
I cannot reproduce it with minimal code not sure what was the problem. but I can confirm that this problem is only occurred when use node v12

@WilianZilv
Copy link
Author

Same here, i can't reproduce the issue with a minimal example

@IBMRob
Copy link

IBMRob commented Jan 7, 2020

We think this might be a problem with express where it doesn't allow minification of comments i.e. it has:

/*!
 * express
 * Copyright(c) 2009-2013 TJ Holowaychuk
 * Copyright(c) 2013 Roman Shtylman
 * Copyright(c) 2014-2015 Douglas Christopher Wilson
 * MIT Licensed
 */

at the top of all its .js files. If you replace the /*! with /** we no longer see the problem.

@WilianZilv
Copy link
Author

But the express module itself is not compiled to bytenode

@IBMRob
Copy link

IBMRob commented Jan 7, 2020

Agreed. I can only post what we have found to fix it.

@OsamaAbbas
Copy link
Collaborator

We think this might be a problem with express where it doesn't allow minification of comments i.e. it has:

/*!
 * express
 * Copyright(c) 2009-2013 TJ Holowaychuk
 * Copyright(c) 2013 Roman Shtylman
 * Copyright(c) 2014-2015 Douglas Christopher Wilson
 * MIT Licensed
 */

at the top of all its .js files. If you replace the /*! with /** we no longer see the problem.

This is strange, because v8 minify the code before compiling it, and I don't think it respects this /*! ... */ rule.

It is also strange because I have compiled express before, never had any problems.

But at least we have now a concret case to test and try. Thank you.

@OsamaAbbas
Copy link
Collaborator

We think this might be a problem with express where it doesn't allow minification of comments i.e. it has:

/*!
 * express
 * Copyright(c) 2009-2013 TJ Holowaychuk
 * Copyright(c) 2013 Roman Shtylman
 * Copyright(c) 2014-2015 Douglas Christopher Wilson
 * MIT Licensed
 */

at the top of all its .js files. If you replace the /*! with /** we no longer see the problem.

I tried to reproduce this issue and failed again. Can you provide me with your complete setup (with a simple "hello world" server), that reproduce the error? @IBMRob

@IBMRob
Copy link

IBMRob commented Jan 14, 2020

So unfortunately although this change worked for us for a few days, suddenly we are starting to hit it again so the fix above certainly isn't a fix :(

We have not managed to get a simple re-create yet. Its certainly a Node12 issue as moving back to Node10 works-around the problem.

@a-parser
Copy link

Hi, I guess you get this problem due to bytecode flushing, just try this solution:

const v8 = require('v8');
v8.setFlagsFromString('--no-flush-bytecode');

@OsamaAbbas
Copy link
Collaborator

Hi, I guess you get this problem due to bytecode flushing, just try this solution:

const v8 = require('v8');
v8.setFlagsFromString('--no-flush-bytecode');

We have discussed flushing here: #36 . It seems that my previous analysis was wrong though.

Thank you for your comment. I hope @WilianZilv and @IBMRob find that flag useful for their issue.

@greenimpala
Copy link

I was having the same issues described in this thread too.
Setting --no-flush-bytecode as suggested by @a-parser seems to have fixed it for me.

@OsamaAbbas
Copy link
Collaborator

Fine. I will wait until WilianZilv and IBMRob responde, then I will publish a new version that sets this flag in bytenode itself. I will update README.md too to reflect this issue.

@OsamaAbbas
Copy link
Collaborator

I will wait for two more days or so, then I will close this issue due to inactivity.

@IBMRob
Copy link

IBMRob commented Jan 26, 2020

Using --no-flush-bytecode on the bytenode command line has also resolved this issue for us too. If there is a way to always do this so we don't need to have custom command lines it would be good

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants