nuxt-community / express-template

Starter template for Nuxt 2 with Express.
https://codesandbox.io/s/github/nuxt-community/express-template
1.25k stars 239 forks source link

Is it possible to use aliases ~ in express? #130

Closed JkmAS closed 6 years ago

JkmAS commented 6 years ago

Hi, I am using nuxt-edge with express server via nuxt.config.js serverMiddleware and I want to use aliases (~, ~~) in imports in the same way as on the frontend (e. g. in vue templates).

Is it even possible to use them? Because in this repo they are not used https://github.com/nuxt-community/express-template/blob/master/template/api/index.js#L7.

Thanks


If I use alias, the nuxt throws an error Cannot find module '~/router/router'.

Works: import router from './router/router'; Doesn't work import router from '~/router/router';

express.js index file

'use strict';

import express from 'express';
import bodyParser from 'body-parser';

import router from '~/router/router';
import log from '~/helpers/logger';

const app = express();

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());

app.use(router);

// catch pm2 shutdown signal
process.on('SIGINT', () => {
  log.info('Service graceful shutdown');
});

export default {
  path: '/api',
  handler: app
};

nuxt.config.js

export default {
  rootDir: __dirname, 
  router: {
    middleware: 'lang'
  },
  // server api
  serverMiddleware: ['~/server/index.js'],
  axios: {
    browserBaseURL: '/'
  },
  modules: [
    '@nuxtjs/axios',
    '@nuxtjs/pwa'  
    ]
  ]
};
This question is available on Nuxt community (#c108)
ghost commented 6 years ago

This issue as been imported as question since it does not respect express-template issue template. Only bug reports and feature requests stays open to reduce maintainers workload. If your issue is not a question, please mention the repo admin or moderator to change its type and it will be re-opened automatically. Your question is available at https://cmty.app/nuxt/express-template/issues/c108.

gamelaster commented 6 years ago

Since there is no backpack anymore, you can't use ES6 Modules in express

JkmAS commented 6 years ago

No, I am using nuxt-edge (check this link https://medium.com/nuxt/nuxt-2-is-coming-oh-yeah-212c1a9e1a67#5234 -- Nuxt ES modules). So the question isn't related to using ES6 modules.

I have an issue with aliases. They don't work in server code. Maybe it's issue for nuxt-edge, I don't know.