typescript icon indicating copy to clipboard operation
typescript copied to clipboard

nuxtServerInit is not called when using express.js

Open smoonsf opened this issue 7 years ago • 6 comments

Describe the bug nuxtServerInit is not called when using express.js

To Reproduce Steps to reproduce the behavior:

  1. Clone repository 'https://github.com/smoonsf/nuxt-typescript-express-vuex-bug'
  2. Run 'yarn && yarn run dev'
  3. See error
  • I put console in nuxtServerInit in @/store/index.ts so log should be shown in terminal console.

Expected behavior nuxtServerInit should be called well.

Additional context It goes well in nuxt-ts but i need custom node server. I couldn't find how to solve this issue

smoonsf avatar Nov 28 '19 11:11 smoonsf

https://github.com/nuxt/typescript/issues/193

I found the answer in here. Sorry for bothering.

smoonsf avatar Nov 28 '19 11:11 smoonsf

https://github.com/nuxt/typescript/issues/193#issuecomment-554298475

It use older version of @nuxt/typescript-runtime i cannot find @nuxt/typescript-runtime/lib

smoonsf avatar Nov 28 '19 11:11 smoonsf

@smoonsf Did you try to register ts-node yourself in your express server setup ? That's mostly what @nuxt/typescript-runtime does.

kevinmarrec avatar Nov 28 '19 13:11 kevinmarrec

@kevinmarrec No, i didn't. I just try to use express.js as custom node server.

const express = require('express');
const consola = require('consola');
const { Nuxt, Builder } = require('nuxt');
const app = express();

// Import and Set Nuxt.js options
const config = require('../nuxt.config.js');
config.dev = process.env.NODE_ENV !== 'production';

async function start () {
  // Init Nuxt.js
  const nuxt = new Nuxt(config);

  const { host, port } = nuxt.options.server;

  // Build only in dev mode
  if (config.dev) {
    const builder = new Builder(nuxt);
    await builder.build();
  } else {
    await nuxt.ready();
  }

  // Give nuxt middleware to express
  app.use(nuxt.render);

  // Listen the server
  app.listen(port, host);
  consola.ready({
    message: `Server listening on http://${host}:${port}`,
    badge: true
  })
}
start();

https://github.com/smoonsf/nuxt-typescript-express-vuex-bug/blob/master/server/index.js

smoonsf avatar Nov 29 '19 00:11 smoonsf

@smoonsf Well If i'm right, even in dev mode you need await nuxt.ready(); so it correctly load modules specified in your Nuxt config (which should include @nuxt/typescript-build module, which is responsible of handling store/*.ts files)

There will be proper documentation for the programmatic usage soon.

Related : https://github.com/nuxt/typescript/issues/139

kevinmarrec avatar Nov 29 '19 09:11 kevinmarrec

@kevinmarrec you're right. it works well with await nuxt.ready();. But it is weird that yarn create nuxt-app create server/index.js like this. It should be fixed i think.

smoonsf avatar Dec 02 '19 01:12 smoonsf