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

File extension required in serverMiddleware #80

Closed
llienard opened this issue Aug 22, 2019 · 8 comments · Fixed by #97
Closed

File extension required in serverMiddleware #80

llienard opened this issue Aug 22, 2019 · 8 comments · Fixed by #97
Assignees
Labels
bug Something isn't working runtime The issue or pull request is related to `runtime` package

Comments

@llienard
Copy link

llienard commented Aug 22, 2019

Describe the bug
File extension .ts is required in nuxt.config.ts serverMiddleware

To Reproduce
Steps to reproduce the behavior:

  1. Create a nuxt typerscript project
  2. Create a file server/middleware.ts
  3. Set it to nuxt.config.ts like
{
serverMiddleware: [
    '~/server/middleware' // without extension
  ]
}
  1. Build with node ./node_modules/\@nuxt/typescript-runtime/bin/nuxt-ts.js build
  2. Start with node ./node_modules/\@nuxt/typescript-runtime/bin/nuxt-ts.js start
  3. See error

Error: Cannot find module '~/server/middleware'

Expected behavior
This should work

@kevinmarrec
Copy link
Contributor

@llienard I'm aware of this issue, thanks for creating it though !

The thing is that for now it's the typescript-build module that tells Nuxt looking for .ts extensions for serverMiddlewares. But as the module is not registered when using nuxt-ts start (cause it's a buildModule), it doesn't resolve .ts automatically.

Current workaround is moving @nuxt/typescript-build from buildModules to modules for now.
We're working on a feature around Nuxt core to be able to fix this issue.

@llienard
Copy link
Author

Thanks for your reply @kevinmarrec

I understand the issue but it would be nice to add a note to the documentation because the nuxt-ts dev command works when server middlewares are set without extension but the nuxt-ts start doesn't. It's very confusing :(

@kevinmarrec kevinmarrec added the bug Something isn't working label Aug 23, 2019
@kevinmarrec
Copy link
Contributor

kevinmarrec commented Aug 29, 2019

Workarounds

Workaround N°1
Set @nuxt/typescript-build in modules instead of buildModules

Workaround N°2 (Recommended)
Force ts extension in extensions array of configuration :

export default {
  extensions: ['ts']
}

Workaround N°3
Force ts extension in extensions array of configuration :

export default {
  extensions: ['ts']
}

Fix proposal

Use new CLI hooks (starting Nuxt 2.9.1), especially config one, to make nuxt-ts ensure that ts extension is registered, even when using nuxt-ts start.

@kevinmarrec kevinmarrec self-assigned this Aug 29, 2019
@kevinmarrec kevinmarrec added the runtime The issue or pull request is related to `runtime` package label Aug 30, 2019
@llienard
Copy link
Author

llienard commented Sep 6, 2019

Hi, I have an other problem with alias path from typescript configuration.

When I import a file via an alias from a server middleware, I have an error on nuxt-ts build

// server/middleware/request-logger.middleware.ts
import logger from '~/utils/ServerLogger'
// ...

Result :

image

@kevinmarrec
Copy link
Contributor

kevinmarrec commented Sep 6, 2019

@llienard Should works if you use .ts extension everywhere.
Still it will be fixed with next Nuxt version & #97

@kevinmarrec
Copy link
Contributor

Will be fixed with next runtime version and Nuxt 2.10 (there will be release note that force user to use Nuxt 2.10 to have the updated TS runtime working)

It will be fixed thanks to this refactor using Nuxt 2.10 hooks : #97

Closing as the PR has been merged.

@andywampir
Copy link

It seems new version doesn't works for me.
I'm still getting error Cannot find module: '~/server/router'.

Versions

nuxt: 2.10.2
@nuxt/typescript-build: 0.3.2
@nuxt/typescript-runtime: 0.2.2
node: 12.13.0

@andywampir
Copy link

It seems new version doesn't works for me.
I'm still getting error Cannot find module: '~/server/router'.

Versions

nuxt: 2.10.2
@nuxt/typescript-build: 0.3.2
@nuxt/typescript-runtime: 0.2.2
node: 12.13.0

I got help in Discord server.
Alias won't be recognized, cause it's runs outside webpack environment.
So in my case i need to use import router from '../server/router'.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working runtime The issue or pull request is related to `runtime` package
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants