5
votes

This is error on cmd npm run serve:ssr

 I'm getting the error 'Can't find module 'require("./server/main") while hit the command "npm run server:ssr".
    but "npm run build:ssr" execute successfully.
    Please Help anyone.

    PS C:\Users\DELL\ion\portfolio\portfolio> npm run serve:ssr

    > [email protected] serve:ssr C:\Users\DELL\ion\portfolio\portfolio
    > node dist/server

    internal/modules/cjs/loader.js:628
        throw err;
        ^

    Error: Cannot find module 'require("./server/main")'
    Require stack:
    - C:\Users\DELL\ion\portfolio\portfolio\dist\server.js
        at Function.Module._resolveFilename (internal/modules/cjs/loader.js:625:15)
        at Function.Module._load (internal/modules/cjs/loader.js:527:27)
        at Module.require (internal/modules/cjs/loader.js:683:19)
        at require (internal/modules/cjs/helpers.js:16:16)
        at webpackUniversalModuleDefinition 
        (C:\Users\DELL\ion\portfolio\portfolio\dist\server.js:3:28)
        at Object.<anonymous> 
        (C:\Users\DELL\ion\portfolio\portfolio\dist\server.js:10:3)
        at Module._compile (internal/modules/cjs/loader.js:776:30)
        at Object.Module._extensions..js 
       (internal/modules/cjs/loader.js:787:10)
        at Module.load (internal/modules/cjs/loader.js:643:32)
        at Function.Module._load (internal/modules/cjs/loader.js:556:12) 
     {
      code: 'MODULE_NOT_FOUND',
      requireStack: [ 
    'C:\\Users\\DELL\\ion\\portfolio\\portfolio\\dist\\server.js' ]
    }

I have also tried to use import instead of const in server.ts file

    npm ERR! code ELIFECYCLE
    npm ERR! errno 1
    npm ERR! [email protected] serve:ssr: `node dist/server`
    npm ERR! Exit status 1
    npm ERR!
    npm ERR! Failed at the [email protected] serve:ssr script.
    npm ERR! This is probably not a problem with npm. There is likely 
    additional logging output above.

    npm ERR! A complete log of this run can be found in:
    npm ERR!     C:\Users\DELL\AppData\Roaming\npm-cache\_logs\2019-08- 
    31T17_48_40_065Z-debug.log

please help me out of this problem this is the last step in angular firebase universal

4
Were you be able to solve this? Im having the same issue. - user2129353
Could you please tell me what tutorial you followed? - user2129353
the tutorial link is here - Aman Gupta
can you help me to use this npm api in Angular CLI - Aman Gupta
sadly, this is the tutorial i followed from the beggining.... :( What do you need help with? - user2129353

4 Answers

1
votes

I was receiving the same error and tracked it down to a couple of lines in webpack.server.config.js

  externals: {
    './dist/server/main': 'require("./server/main")'
  },

commenting those out increased my bundle size a bit, but I no longer receive that error in my Lambda function. I haven't figured out why that causes an error yet, but maybe it will get you going again until we can find out why.

0
votes

everything worked fine with me. I got rid of the error by correcting code in the 'server.js' file.

module.exports = require("require(\"./server/main\")");

to

module.exports = require('./server/main');

i used 'ng-toolkit/universal' and after running 'npm run server' i got same error.

0
votes

Hopefully this also works for you:

externals: [/(node_modules|main\..*\.js)/,],

-1
votes
  externals: {
  //  './dist/server/main': 'require("./server/main")'
  },

Commenting the require line in webpack.server.config.js, handles the error. But no clue why Universal / Serverless / web-pack server auto generated code adding this line in web pack.