question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

"strict mode" doesn't get enabled for nodejs script when runing through forever

See original GitHub issue

I have a script that uses strict mode, it’s an executable script with a “#!/usr/bin/node --harmony” shebang and runs fine when I directly call it from the terminal. I use this line at the start of the script to enable strict mode:

#!/usr/bin/node --harmony
"use strict";

However, if I run it through forever (forever start script_name) I obtain the following error in the logs that causes the script to abort over and over:

        let report = {
        ^^^
SyntaxError: Unexpected strict mode reserved word
  at Module._compile (module.js:439:25)
  at Object.Module._extensions..js (module.js:474:10)
  at Module.load (module.js:356:32)
  at Function.Module._load (module.js:312:12)
  at Function.Module.runMain (module.js:497:10)
  at startup (node.js:119:16)
  at node.js:906:3
error: Forever detected script exited with code: 8

Is there some way for me to indicate to forever that I want to run the nodejs script in strict mode? Or how can I specity commandline arguments for the node binary when running forever?

EDIT: I also tried doing this:

if(process.argv[2] == "start") {
    require("forever").start(__filename, {
           'uid': 'script_name',
           'minUptime': 500,
           'spinSleepTime': 0.5 * 60 * 1000,
           'command': 'node',
           'args': [ '--use_strict', '--harmony' ]
      });
}

This didn’t work either, I get the same result.

EDIT2: Funnily enough, I found it works when I use ‘perl’ as the command interpreter…

if(process.argv[2] == "start") {
    require("forever").start(__filename, {
           'uid': 'script_name',
           'minUptime': 500,
           'spinSleepTime': 0.5 * 60 * 1000,
           'command': 'perl'
      });
}

Looks like perl executes the file correctly after reading the shebang.

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
indexzerocommented, Jul 24, 2015

Yes. Either that or just do what most folks I know do and put "use strict" at the top of every one of your source code files.

0reactions
jmonstercommented, Jul 24, 2015

I figured out the following incantation to make things work; This is working in production for the Heroku dashboard w/iojs:

"scripts": {
  "start": "node ./node_modules/.bin/forever -c 'node --use_strict --harmony_arrow_functions' --spinSleepTime 1 --minUptime 1000 ./bin/www"
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

"strict mode" doesn't get enabled for nodejs script when runing ...
The problem was that "use strict" did not work when using forever, like it's shown in the description it worked when the script...
Read more >
The modern mode, "use strict"
Doing that enables strict mode in that function only. But usually people use it for the whole script. Ensure that “use strict” is...
Read more >
Any way to force strict mode in node? - Stack Overflow
It's worth noting that ESLint enforces strict mode by default. It won't stop you from running node ...
Read more >
Running Node.js scripts continuously using forever
This article demonstrates how to set up forever, an npm module, with a Node application and covers some important forever commands.
Read more >
Strict mode - JavaScript - MDN Web Docs
Strict mode makes several changes to normal JavaScript semantics: Eliminates some JavaScript silent errors by changing them to throw errors.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found