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

UnhandledPromiseRejectionWarning when deploying custom server to now #1018

Closed
jessehattabaugh opened this issue Feb 6, 2017 · 9 comments
Closed

Comments

@jessehattabaugh
Copy link
Contributor

jessehattabaugh commented Feb 6, 2017

In order to use a hostname other than localhost I'm creating a custom server as @arunoda advised in #1010.

const { createServer } = require('http');
const { parse } = require('url');
const next = require('next');

const port = process.env.PORT || 3000;
const hostname = process.env.IP || 'localhost';
const app = next({ dev: process.env.NODE_ENV !== 'production' });
const handle = app.getRequestHandler();

app.prepare().then(() => {
  createServer((req, res) => {
  	const parsedUrl = parse(req.url, true);
  	handle(req, res, parsedUrl);
  })
  .listen(port, hostname, (err) => {
    if (err) throw err;
    console.log(`> Ready on http://${ hostname }:${ port }`);
  });
});

My package.json scripts are as follows

"scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "deploy": "now",
    "build": "next build",
    "start": "node server"
  },

Running $ npm start works as expected, but when I deploy to now using $ npm run deploy I get the following output

npm install
⧗ Installing:
 ‣ next@^2.0.0-beta.24
 ‣ now@^4.2.2
✓ Installed 646 modules [18s]
npm run build

> studynew@0.0.0 build /home/nowuser/src
> next build
npm start

> studynew@0.0.0 start /home/nowuser/src
> node server
(node:233) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: EACCES: permission denied, rmdir '/home/nowuser/src/.next'
(node:233) DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

may be related to #452 or #300

@jessehattabaugh
Copy link
Contributor Author

I tried adding a .catch((err) => console.error(err)) to the promise returned by prepare() and now the error I get when deploying is

> ▲ npm start
> > studynew@0.0.0 start /home/nowuser/src
> > node server
> { Error: EACCES: permission denied, rmdir '/home/nowuser/src/.next'
>   errno: -13,
>   code: 'EACCES',
>   syscall: 'rmdir',
>   path: '/home/nowuser/src/.next' }

@bryanrsmith
Copy link
Contributor

const app = next({ dev: process.env.NODE_ENV !== 'production' });

I think you'll need to use "start": "NODE_ENV=production node server" to switch next out of dev mode.

@jessehattabaugh
Copy link
Contributor Author

Huh, I would've expected Now.js to set NODE_ENV to 'production' the way Heroku does.

I checked for NOW and it works now!

const app = next({ dev: process.env.NODE_ENV !== 'production' && !process.env.NOW });

@rauchg
Copy link
Member

rauchg commented Feb 6, 2017

@jessehattabaugh we didn't want to be surprising. After all, you could launch processes to simulate development, you could run builds, etc. What do you think?

@jessehattabaugh
Copy link
Contributor Author

jessehattabaugh commented Feb 6, 2017

I think it's more intuitive to do NODE_ENV=development when I'm trying to simulate development in my production environment than to have to explicitly declare NODE_ENV=production which seems like it should be the default. If I'm running a build on production I probably want it to be a production build. I sometimes use NODE_ENV to trigger things like minification and watchers in my builds. I feel like the command to start my server should be the same on production and development; $ npm start without having to explicitly set NODE_ENV before running it.

@rauchg
Copy link
Member

rauchg commented Feb 6, 2017

Sounds good. Thanks a lot for your feedback

@timneutkens
Copy link
Member

timneutkens commented Feb 6, 2017

This is only the case when running a custom server. next build and next start both set production as process.env.NODE_ENV if it is not defined by the user.

@bryanrsmith
Copy link
Contributor

FWIW, this exact thing happened to me the first time I deployed a custom server. I also expected now to set NODE_ENV=production.

It makes sense why it's set up this way; I just had to make the mistake once to learn it. I think this specific case could be improved a lot if Next.js could catch this error and log a helpful message. Maybe it could even look at process.env.NOW and tell the user they need to set NODE_ENV.

@headwinds
Copy link

agree with @bryanrsmith if the error could also point to this thread and/or a doc about this issue.

I encountered it after following the custom express example but not as closely as I should have. I missed the yarn dev part and began with yarn start instead which most apps begin with for local dev. Then couldn't figure why I wasn't getting HMR and when I went to deploy it was still in dev mode but this thread sorted everything out.

@lock lock bot locked as resolved and limited conversation to collaborators May 10, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants