Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Webpack is returning __dirname as '/' #143

Closed
Pietrox opened this issue Aug 31, 2019 · 7 comments
Closed

Webpack is returning __dirname as '/' #143

Pietrox opened this issue Aug 31, 2019 · 7 comments

Comments

@Pietrox
Copy link

Pietrox commented Aug 31, 2019


[ ] Regression 
[x ] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request 

Current behavior

When compiling to prod with build:ssr and serve:ssr it seems webpack is returning __dirname as "/" for swagger.

Minimal reproduction of the problem with instructions

Use @nestjs/ng-universal with default webpack config and default swagger config.

Environment

Ubuntu 18.04 and also local on Windows 10.
Node 10.16
Npm 6.9.0
NestJS 6.6.3
NestJS/Swagger 3.1.0
NestJS/ng-universal 2.1.0
Webpack-cli 3.3.7

Exact error im receiving is:
Unhandled Promise rejection: ENOENT: no such file or directory, open '//indexTemplate.html.tpl' ; Zone: <root> ; Task: Promise.then ; Value: { Error: ENOENT: no such file or directory, open '//indexTemplate.html.tpl' at Object.openSync (fs.js:438:3) at Object.readFileSync (fs.js:343:35) at Object.generateHTML (C:\Users\Work\PhpstormProjects\terminal-app\dist\server.js:80659:16) at Function.setupExpress (C:\Users\Work\PhpstormProjects\terminal-app\dist\server.js:79512:39) at Function.setup (C:\Users\Work\PhpstormProjects\terminal-app\dist\server.js:79505:21) at C:\Users\Work\PhpstormProjects\terminal-app\dist\server.js:129:33 at Generator.next (<anonymous>) at fulfilled (C:\Users\Work\PhpstormProjects\terminal-app\dist\server.js:95:58) at Zone.run (C:\Users\Work\PhpstormProjects\terminal-app\dist\server.js:1633:43) errno: -4058, syscall: 'open', code: 'ENOENT', path: '//indexTemplate.html.tpl' } Error: ENOENT: no such file or directory, open '//indexTemplate.html.tpl' at Object.openSync (fs.js:438:3) at Object.readFileSync (fs.js:343:35) at Object.generateHTML (C:\Users\Work\PhpstormProjects\terminal-app\dist\server.js:80659:16) at Function.setupExpress (C:\Users\Work\PhpstormProjects\terminal-app\dist\server.js:79512:39) at Function.setup (C:\Users\Work\PhpstormProjects\terminal-app\dist\server.js:79505:21) at C:\Users\Work\PhpstormProjects\terminal-app\dist\server.js:129:33 at Generator.next (<anonymous>) at fulfilled (C:\Users\Work\PhpstormProjects\terminal-app\dist\server.js:95:58) at ZoneDelegate.invoke (C:\Users\Work\PhpstormProjects\terminal-app\dist\server.js:1874:26) at Zone.run (C:\Users\Work\PhpstormProjects\terminal-app\dist\server.js:1633:43)
I tried doing __dirname: true/false but it seems that webpack-config.factory.d.ts is not accepting this param - so general workaround doesnt work.

@Pietrox
Copy link
Author

Pietrox commented Sep 9, 2019

Nevertheless I did also a topic on stackoverflow. As general fixes are not working with it also. Somehow the normal options to configure webpack are modified and most probably i placed the issue in wrong project as this looks like more like nestjs/ng-universal. @kamilmysliwiec is it more swagger or ng-universal issue ?

@kamilmysliwiec kamilmysliwiec transferred this issue from nestjs/swagger Sep 13, 2019
@kamilmysliwiec
Copy link
Member

I moved this issue under ng-universal repo.

Please, provide a minimal repository which reproduces your issue.

@Pietrox
Copy link
Author

Pietrox commented Sep 17, 2019

Hello Kamil,

I think I know the issue. Swagger in general has problems with webapck and production serve according to this issue swagger-ui/#90, but the ng-universal bundling is not accepting the configs defined in webpack config.

All works fine if disabling swagger in prod.

My webpack config is:

const webpack = require('webpack');
const WebpackConfigFactory = require('@nestjs/ng-universal')
  .WebpackConfigFactory;

module.exports = WebpackConfigFactory.create(webpack, {
  // Nest server for SSR
  server: './server/main.ts',
}); 

My main.ts init on swagger is:

  const swaggerOptions = new DocumentBuilder()
      .setTitle('NestJS API')
      .setDescription('Swagger')
      .setVersion('0.0.1')
      .setHost(hostDomain.split('//')[1])
      .setSchemes(ApplicationModule.devMode ? 'http' : 'https')
      .setBasePath('/api')
      .addBearerAuth('Authorization', 'header')
      .build();
  const swaggerDoc = SwaggerModule.createDocument(app, swaggerOptions);

  SwaggerModule.setup('/api/docs', app, swaggerDoc, {
      swaggerUrl: `${hostDomain}/api/docs-json`,
      explorer: true,
      swaggerOptions: {
        docExpansion: 'list',
        filter: true,
        displayRequestDuration: true,
      },
    });

@kamilmysliwiec
Copy link
Member

the ng-universal bundling is not accepting the configs defined in webpack config.

Why not? You can simply modify the object returned by WebpackConfigFactory.create()

@Pietrox
Copy link
Author

Pietrox commented Sep 27, 2019

Well I tried placing :

'__dirname': false

inside it, but when compiling to prod it throws:

Invalid configuration object. Webpack has been initialised using a configuration object that does not match the API schema. - configuration.entry should be one of these: function | object { <key>: non-empty string | [non-empty string] } | non-empty string | [non-empty string] -> The entry point(s) of the compilation.

Details: * configuration.entry['node'] should be a string. -> The string is resolved to a module which is loaded upon startup.
* configuration.entry['node'] should be an array: [non-empty string] -> A non-empty array of non-empty strings
* configuration.entry['node'] should be one of these: [non-empty string] -> All modules are loaded upon startup. The last one is exported.
* configuration.entry['node'] should be one of these: non-empty string | [non-empty string] -> An entry point with name

@kamilmysliwiec
Copy link
Member

This error is thrown by webpack (invalid schema). I don't think that it is related to this package.

@hungtcs
Copy link

hungtcs commented Nov 1, 2019

I have same problam, change webpack config can solve this problem

const merge = require('webpack-merge');
const webpack = require('webpack');
const { WebpackConfigFactory } = require('@nestjs/ng-universal');

module.exports = merge(WebpackConfigFactory.create(webpack, {
  // Nest server for SSR
  server: './server/main.ts'
}), {
  node: {
    __dirname: false, // <-------------------------
    __filename: false,
  },
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants