3

I'm using renderToString function from react-dom (on my server side). The code looks like (+/-):

import Home from './app/containers/Home';
const app = express();

app.get('**', (req, res) => {
  const html = renderToString(<Home />);
  res.set('Cache-Control', 'public, max-age=600, s-maxage=1200');
  res.send(html);
});

Everything goes smooth, until I try to deploy it on the server.

Example error in the console:

Error: Error parsing triggers: Cannot find module 'store/Home/actions'

When it appears?

If I change my path to some other component, which does not use any other components (only modules from node, like react or react-redux) it works correctly.

But if I try to use some component which uses other components and imports them, e.g.:

var _CreateUser = require('components/Pages/CreateUser'); (it's in the rendered component)

Now it will fail with error:

Error: Error parsing triggers: Cannot find module 'components/Pages/CreateUser'

So currently Im stuck, because I have to use my whole app on server side, not just a single component which doesn't import anything :)

Why does it work this way? Why does it fail? Is it bad webpack config fail?

Looking forward for any help. Thank you.

Note, as I said above, if I render to string some component with any imports (that doesn't use any other component in it) - the server side rendering works fine and Im able to see the renderedToString content before page loads.

enter image description here

3
  • 1) What OS in your server and your local/dev computer? 2) did you installed all packages at server side (the same version as in your dev enviroment)? Commented Mar 5, 2018 at 15:48
  • How do you deploy? Just copy paste folder? Or do you use git? Commented Mar 5, 2018 at 15:54
  • @sultan 1) Local win7. Server? I don't really know... 2) I've installed all packages that im using on server side rendering. 3) I deploy it with firebase deploy, Im not using git Commented Mar 7, 2018 at 19:45

3 Answers 3

3
+25

Everywhere you import local modules you need to include the directory in the path otherwise it will search node_modules for a named package and ultimately fail.

require('./store/Home/actions');

Or:

import HomeActions from './store/Home/actions';

...depending on which import style you're using. An accurate directory is always needed as a part of the import/require statement.

Sign up to request clarification or add additional context in comments.

4 Comments

Same thing happens. Cannot find module './components/Pages/CreateUser'
Could you show the code for CreateUser? If not check out the export statement. If you're using require (commonJS) it should be module.exports = something or if you're using import (es2015+) you'd need to export with export default myModuleName or something similar. They're not interchangeable. [medium.com/@brianleroux/… Article on that topic)
Sorry for the delay. So at the end I have exports.default = enhance(CreateUser); and at the beginning Object.defineProperty(exports, "__esModule", { value: true });
Okay, that seems to be the problem. You shouldn't need the defineProperty and you're mixing commonJS modules with es6 imports. Here's a good page that shows the difference. require vs. import
2

You're using a relative path, cd into and deploy from the functions directory so it's correct.

1 Comment

I still get the same error :( I tried to deploy from like every possible directory...
2

It looks like your Home component is inside functions/app/containers/Home and you need access to the file functions/app/store/Home/actions.

From your containers/Home file, you need to go up two directories to the app folder, then go down two directories to correct file. So

import HomeActions from '../../store/Home/actions'`. 

Each ../ represents going up one directory to the parent folder. We went from

functions/app/containers/Home to

functions/app/containers/ to

functions/app and then we can specify which path to continue on from there with

store/Home/actions

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.