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.

API Routes return 404 "This page could not be found"

See original GitHub issue

Bug report

Describe the bug

I have just upgraded to v9.0.2 and I want to implement API Routes as part of my solution. When I follow the documentation and navigate to the path where the API Route should be, I get the default 404 page with “This page could not be found”.

To Reproduce

I have followed the example in the examples folder, so I’ve added the path /pages/api/people/index.ts.

Screenshot 2019-07-30 at 16 35 51

The contents of this file is

import { NextApiRequest, NextApiResponse } from 'next';

export default (req: NextApiRequest, res: NextApiResponse) => {
  res.status(200).json({ title: 'Next.js' });
};

I then start the app and navigate to http://localhost:3000/api/people where I see the 404 page

Screenshot 2019-07-30 at 16 37 35

You’ll probably also want to know what is in my next.config.js file so that looks like this

const withPlugins = require('next-compose-plugins');
const withSass = require('@zeit/next-sass');
const withCss = require('@zeit/next-css');
const withBundleAnalyzer = require('@zeit/next-bundle-analyzer');
const withImages = require('next-images');
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');

require('dotenv').config();

const {
  ANALYZE_BUNDLE: analyzeBundle,
  IS_LOCAL: isLocal,
  STAGE: stage,
} = process.env;

module.exports = withPlugins(
  [
    [withImages],
    [withCss],
    [
      withSass,
      {
        cssModules: true,
        cssLoaderOptions: {
          importLoaders: 1,
          localIdentName: isLocal ? '[name]__[local]--[hash:base64:6]' : '[hash:base64:6]',
        },
      },
    ],
    [
      withBundleAnalyzer,
      {
        analyzeServer: ['server', 'both'].includes(analyzeBundle),
        analyzeBrowser: ['browser', 'both'].includes(analyzeBundle),
        bundleAnalyzerConfig: {
          server: {
            analyzerMode: 'server',
            analyzerPort: 8888,
          },
          browser: {
            analyzerMode: 'server',
            analyzerPort: 8889,
          },
        },
      },
    ],
  ], {
    env: {
      STAGE: stage,
    },
    target: 'serverless',
    pageExtensions: ['tsx'],
    webpack(config, options) {
      if (options.isServer) {
        // Type checking on build
        config.plugins.push(new ForkTsCheckerWebpackPlugin());
      }

      return config;
    },
  },
);

Also worth noting that since updating to v9 I see this in the console output when I build.

Found experimental config:
Experimental features can change at anytime and aren't officially supported (use at your own risk).

I don’t know what is considered experimental about my configuration, but perhaps this could be related?

Expected behavior

I expect to see a JSON response of

{ title: 'Next.js' }

Screenshots

See above

System information

  • OS: MacOS
  • Browser Chrome, Postman
  • Version of Next.js: 9.0.2

Additional context

The example in the examples folder works for me and I’ve copied that as closely as I can. I do think it’s something with my solution but I can’t figure out the root cause. Also I can’t provide a repository as it’s market sensitive but I’m happy to provide code samples as needed.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

6reactions
mattdellcommented, Aug 5, 2019

Sorry for the delay, but I got sidetracked with some other work. But - I’ve figured it out!

Removing this line in my next.config.js solves the problem

pageExtensions: ['tsx'],

It makes sense given that my filename was index.ts

Funny enough it’s very related to #6878 which I raised ages ago. I’m assuming since Next v9 natively supports TypeScript I no longer need to specify the extension.

1reaction
Timercommented, Jul 30, 2019

Unrelated, but be sure to remove ForkTsCheckerWebpackPlugin from your webpack configuration. This is already handled in Next.js 9.

Read more comments on GitHub >

github_iconTop Results From Across the Web

NextJS API route throwing 404 - Stack Overflow
The problem is that it's returning 404 and erroring all the time. I already confirmed that the API route itself works fine when...
Read more >
Dynamic Routes Details - Next.js
If fallback is false , then any paths not returned by getStaticPaths will result in a 404 page. If fallback is true ,...
Read more >
Laravel 5.5 always keep returning the 404 view blade when ...
Everytime I tried to post request to an api route the response always Sorry, the page you are looking for could not be...
Read more >
Requesting Next.js web service through public route returns ...
Strapi on /api is working including the admin panels and requests. ... is accessible through the /sporus route and does not return a...
Read more >
How to wrap a route not found exception with the generic ...
The status code 404 is also added to the response when the resource is not found, like when Id is not found or...
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