nuxtServerInit is not called when using express.js
Describe the bug nuxtServerInit is not called when using express.js
To Reproduce Steps to reproduce the behavior:
- Clone repository 'https://github.com/smoonsf/nuxt-typescript-express-vuex-bug'
- Run 'yarn && yarn run dev'
- 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
https://github.com/nuxt/typescript/issues/193
I found the answer in here. Sorry for bothering.
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 Did you try to register ts-node yourself in your express server setup ? That's mostly what @nuxt/typescript-runtime does.
@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 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 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.