fridays / next-routes

Universal dynamic routes for Next.js
MIT License
2.47k stars 230 forks source link

routes.ts is not a module. How to fix this message on typescript? #133

Closed johhansantana closed 6 years ago

johhansantana commented 6 years ago
image

I'm using typescript with nextjs but this error shows up. The library works though.

centinel3 commented 6 years ago

+1

Cannot find module './routes'

and on the client when using : [ts] Module '"/Users/chriscampbell/Desktop/ef prototype/routes"' has no exported member 'Link'.

disintegrator commented 6 years ago

I just opened #153 to get typings included in this project :)

You will need to rewrite your code like so:

import routes from '../routes';

const { Link, Route } = routes;

Since routes is an instance of class Routes (see here), its members don't get exported as module exports. It's possible to destructure in vanilla JS but TypeScript is stricter about modules.

fridays commented 6 years ago

Thanks, it's merged now.

ryandrewjohnson commented 5 years ago

I'm using 1.4.2 and still getting the has no exported member 'Link' error from TypeScript when trying to import Link.

import { Link } from '../../server/routes';

Was this actually fixed of do I need to use @disintegrator's workaround still?

fractefactos commented 5 years ago

I'm using 1.4.2 and still getting the has no exported member 'Link' error from TypeScript when trying to import Link.

import { Link } from '../../server/routes';

Was this actually fixed of do I need to use @disintegrator's workaround still?

I'm trying to do this with version 1.4.2, and i confirm is still needed the solution of @disintegrator.

olivererdmann commented 5 years ago

I'm using 1.4.2 and still getting the has no exported member 'Link' error from TypeScript when trying to import Link.

import { Link } from '../../server/routes';

I was getting the same error, but was able to fix it by providing additional JSDoc annotations in routes.js. This helps TS to figure out the exported types, based on the type-definitions provided by next-routes.

// routes.js

/**
 * @typedef { import("next-routes").Registry } Registry}
 */
const routes = require('next-routes');

/**
 * @type {Registry}
 */
module.exports = routes()
    .add('/:slug', 'page');

The import-syntax is specific to TypeScript and described here in detail: https://github.com/Microsoft/TypeScript/wiki/JSDoc-support-in-JavaScript#import-types