Reputation: 13
I am working with Nuxt.js module that adds a plugin if process.server is true, but it does not work. I've tried to log process.server with typescript module
export default function (moduleOptions?: any) {
console.log(process.server);
};
It shows:
yarn run v1.17.3
$ nuxt-ts
undefined 22:00:16
╭──────────────────────────────────────────╮
│ │
│ Nuxt.js v2.9.2 │
│ Running in development mode (spa) │
│ │
│ Listening on: http://localhost:3000/ │
│ │
╰──────────────────────────────────────────╯
How can I fix it?
Upvotes: 1
Views: 1484
Reputation: 4639
From the docs:
Modules are simply functions that are called sequentially when booting Nuxt.
In other words, modules are always called from the server, and are called while setting up the Nuxt instance. For this reason, process.server
is undefined because Nuxt has not yet defined it.
You can instead rely on name conventional plugins, which use a client
or server
postfix in the filename to determine where they should be run. The following example shows how to add the plugin from a module using this:
import path from 'path'
export default function (moduleOptions) {
// Register your plugin here with .server in the name
// to only run on the server. This expects the plugin file to
// be located next to this module file
this.addPlugin(path.resolve(__dirname, 'plugin.server.ts'))
}
Upvotes: 2