Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Node.JS Node.js application fails to start

ac1dr41n

New Coder
I'm having a challenge with a Node application that wont start on one server, but it starts just fine on another server. I thought they were identical, but clearly something is different and I can't figure out what.

SETUP:
Both using Node 14.18.2 (yes I know its older, but can't upgrade right now)
NPM version is 6.14.15
Next.js 11.1.2

We are using PM2 to start the application, although I don't believe the issue is related to PM2.

Typical build and start routine:
* npm install
* npm next build
* pm2 start ecosystem.js

The build runs just fine, but when starting up node on one server, the npm log shows this error:

Code:
9 verbose lifecycle [email protected]~start: CWD: /var/www/app-name/135904dbe0f8c312a65e175671a6d2d5b165afa6
10 silly lifecycle [email protected]~start: Args: [ '-c', 'next start' ]
11 silly lifecycle [email protected]~start: Returned: code: 1  signal: null
12 info lifecycle [email protected]~start: Failed to exec start script
13 verbose stack Error: [email protected] start: `next start`
13 verbose stack Exit status 1
13 verbose stack     at EventEmitter.<anonymous> (/home/ubuntu/.nvm/versions/node/v14.18.2/lib/node_modules/npm/node_modules/npm-lifecycle/index.js:332:16)
13 verbose stack     at EventEmitter.emit (events.js:400:28)
13 verbose stack     at ChildProcess.<anonymous> (/home/ubuntu/.nvm/versions/node/v14.18.2/lib/node_modules/npm/node_modules/npm-lifecycle/lib/spawn.js:55:14)
13 verbose stack     at ChildProcess.emit (events.js:400:28)
13 verbose stack     at maybeClose (internal/child_process.js:1058:16)
13 verbose stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:293:5)

But it works on the other server. I even verified the versions of all the libraries using "npm list". Dump the output from both servers to a file and ran a diff. All versions of all libraries are identical.

I'm a bit stuck. Any assistance on what this might be or where to look next?

Thanks!
 
Line 4 says Failed to exec start script which isn't very helpful. First thing I would always do in a case like this is run the command in question under strace and look for any errors. If you don't know strace yet, invest a little time in it, it's really a lifesaver.
 
Thank you for that guidance. It was helpful. I didn't actually use strace. But I was able to narrow down the start command itself. I ran that and it seemed to work:

Code:
npx next start -p 3000
ready - started server on 0.0.0.0:3000, url: http://localhost:3000
info  - Loaded env from /var/www/app-name/135904dbe0f8c312a65e175671a6d2d5b165afa6/.env
Warning: For production Image Optimization with Next.js, the optional 'sharp' package is strongly recommended. Run 'yarn add sharp', and Next.js will use it automatically for Image Optimization.
Read more: https://nextjs.org/docs/messages/sharp-missing-in-production

After 30 seconds or so, the application is available in the browser with no issue.

So the issue seems to be starting the application from PM2. The only real difference between the servers is that one is using a soft link named "current" that points to a directory named after git commit hash (capistrano-style deployments). The other server is a test server and has a static folder without the soft link.

It seems that on startup, npm is detecting the soft link, and reversing the target environment back to its actual full absolute path WITHOUT the soft link, but that is confusing next.js, which then fails to load pages.

I checked the ecosystem.config.js file. I modified the start command to include a prefix to the specific directory where I wanted the app to start. The ecosystem.config.js file now looks like this:

Code:
module.exports = {
  apps: [
    {
      name: "fe",
      script: "npm",
      cwd: "/var/www/app-name/current",
      args: "start --prefix /var/www/app-name/current",
      instances: 4,
      exec_mode: "cluster",
    }
  ]
};

And that seemed to work!

The EventEmitter error is still in the npm logs, but that seems like a red herring and is unrelated.

Thanks for the assistance!
 
Good work ! You might have spotted that fiddling with the path in strace too. I admit being very fond of that program and its Windows counterpart Process Monitor 😁
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom