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.

can't import TS in Nuxt serverMiddleware

See original GitHub issue

Using 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:closed
  • Created 3 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
pi0commented, Dec 29, 2020

Hi @danielroe I’ve finally checked repro using JITI_DEBUG it seems api/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 😃

1reaction
pi0commented, Nov 23, 2020

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.

Read more comments on GitHub >

github_iconTop 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 >

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