1

I used Razzle for a server side rendered react app.

Now its time to deploy the application on server. I use IIS as web server. But when i move the build folder to another directory and run node server on it there are errors that i cant find the modules i used in my app, like react.

internal/modules/cjs/loader.js:638
    throw err;
    ^
Error: Cannot find module 'react'
...

Should i move the node_modules folder with build folder? or am i doing something wrong?

defectivepixel
  • 575
  • 1
  • 4
  • 22

2 Answers2

6

Since nobody answered my question. I share what i found out.

Razzle use webpack-node-externals to exclude node_modules on build. (i dont know why)

A simple change in razzle.config.js would fix this.

module.exports = {
    modify: (config, { target, dev }, webpack) => {
      if (target === "node") {
        config.externals = []    
      }
      return config;
    },
  };
defectivepixel
  • 575
  • 1
  • 4
  • 22
  • This works. If running in a CI pipeline, you must set process.env.CI to false to avoid a failure, as stated on Sep 12, 2019 comment on this github issue: https://github.com/jaredpalmer/razzle/issues/40#issuecomment-530907082 – Matt Rabe Jan 14 '20 at 17:40
2

You need to move node_modules along with build folder. Because server.js and bundle files in build folder needs the modules that you have installed. The above error occurs because the server can't found the react module.

rehan007
  • 91
  • 1
  • 8