can't import TS in Nuxt serverMiddleware
See original GitHub issueUsing jiti
with createRequire
in Nuxt, I encountered the following issue:
ERROR ServerMiddleware Error: Cannot use import statement outside a module 0
(function (exports, require, module, __filename, __dirname) { import qs from 'querystring'
^^^^^^
SyntaxError: Cannot use import statement outside a module
at new Script (vm.js:88:7)
at d._moduleCompile (/my/site/node_modules/jiti/dist/jiti.js:2:25464)
at Module.n._compile (/my/site/node_modules/jiti/dist/jiti.js:2:25015)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10)
at Module.load (internal/modules/cjs/loader.js:985:32)
at Function.Module._load (internal/modules/cjs/loader.js:878:14)
at Module.require (internal/modules/cjs/loader.js:1025:19)
at require (internal/modules/cjs/helpers.js:72:18)
at Resolver.requireModule (/my/site/node_modules/@nuxt/core/dist/core.js:617:26)
at Server._requireMiddleware (/my/site/node_modules/@nuxt/server/dist/server.js:803:39)
It reproduced consistently whenever using jiti
for createRequire
and going away whenever reverting to esm
. It was solved by clearing node_modules/.cache
and I have hitherto been unable to reproduce on a new install of Nuxt, even with the same/similar source files: see https://github.com/danielroe/jiti-h2-tests
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (4 by maintainers)
Top Results From Across the Web
Is it possible to use typescript in nuxt serverMiddleware?
Yes, just import { ServerMiddleware } from '@nuxt/types'.
Read more >The serverMiddleware Property - Nuxt
Nuxt internally creates a connect instance that you can add your own custom middleware to. This allows us to register additional routes (typically...
Read more >Advanced Features: Middleware - Next.js
You can set request and response headers using the NextResponse API (setting request headers is available since Next.js v13.0.0). // middleware.ts import { ......
Read more >Creating Server-side Rendered Vue.js Apps Using Nuxt.js
In this article, Toptal Freelance Front-end Engineer Ben Jones introduces us to Nuxt.js, a server-side rendering library for Vue.js, inspired by the popular ......
Read more >@whitetrefoil/msm - npm
A Koa middleware of mock server to help develop web app. How To Use (the Simplest Example). Gulpfile.js: import gulp from ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hi @danielroe I’ve finally checked repro using
JITI_DEBUG
it seemsapi/ts.ts
is not passing through jiti so this bug is/was from nuxt/typescript side which doesn’t add.ts
to default supported extensions.Trying with 2.15 (
"nuxt-edge": "^2.15.0-26820047.b2e38f5c"
) everything works good 😃It is impossible that cache get’s stalled since we check against checksum of file contents. With
nuxt-ts
, we disable require-engine (since esm was confliciting). I think that could be the root casue.