question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

webpack resolve.root setup with 'cant find module' issue only on server side

See original GitHub issue

This is a react project with webpack for bundling jsx file, and the folder tree is list as follows:

.
|-- app
|   |-- client.jsx
│   |-- components
|     | -- ArticleCommentListItem.jsx
|     | -- ArticleCommentListItemFooter.jsx
|-- server
|   | -- index.js
|-- webpack
|   |-- webpack.config.js

At first, File ArticleCommentListItem.jsx will invoke module ArticleCommentListItemFooter.jsx like below

import ArticleCommentListItemFooter from './ArticleCommentListItemFooter';

After reading the the answer from Alex Klibisz on Resolving require paths with webpack. I want to have a try to call module as format :

import ArticleCommentListItemFooter from 'components/ArticleCommentListItemFooter';

Then I setup in the webpack.config.js file

var webpack_config = {
  entry:{
    app: path.join(__dirname, '..', 'app', 'client')
  },
  output: {
    path: assetsPath,
    filename: '[name].js',
    publicPath: '/assets/'
  },
  resolve: {
    extensions: ['', '.js', '.jsx', '.scss', '.less'],
    root: path.resolve(__dirname, '..', 'app'),
    modulesDirectories: [
      'node_modules'
    ]
  }
}

Everything runs well when bundle client.jsx to app.js, but when I start server with command node server/index.js. It will exit with error

error: Cannot find module 'components/ArticleCommentListItemFooter'
    at Function.Module._resolveFilename (module.js:338:15)
    at Function.Module._load (module.js:289:25)
    at Module.require (module.js:366:17)
    at require (module.js:385:17)
    at Object.<anonymous> (/Users/ryu/Documents/git/react/react-blog/app/components/ArticleCommentListItem.jsx:21:37)
    at Module._compile (module.js:425:26)
    at loader (/Users/ryu/Documents/git/react/react-blog/node_modules/babel-register/lib/node.js:128:5)
    at Object.require.extensions.(anonymous function) [as .jsx] (/Users/ryu/Documents/git/react/react-blog/node_modules/babel-register/lib/node.js:138:7)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:313:12)

I have also tried to use resolve.alias for webpack.config setup, it still exit with the same error.

resolve: {
  extensions: ['', '.js', '.jsx', '.scss', '.less'],
  alias: {
    components: path.join(__dirname, '..', 'app', 'components')
  },
  modulesDirectories: [
    'node_modules', 'app'
  ]
}

It seems it could not work only on server side. I think it should be the server not aware of webpack config. You can refer to the project on link https://github.com/ryuever/redux-blog

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:9
  • Comments:10 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
mochengcommented, Apr 28, 2016

Same issue in my machine.

npm webpack version is 1.12.14.

This issue makes so-called resolve.alias and resolve.root totally useless!!!

0reactions
vankopcommented, Aug 9, 2020

should work with webpack@latest (probably you need target=node) . Feel free to report issue with reproducible repo, if not.

Read more comments on GitHub >

github_iconTop Results From Across the Web

webpack resolve.root setup with 'cant find module' issue
Your server/index.js has no idea about the existence of either of those values. So it doesn't know how to find 'components/something.jsx'.
Read more >
Resolve | webpack
Configure how modules are resolved. For example, when calling import 'lodash' in ES2015, the resolve options can change where webpack goes to look...
Read more >
Documentation - Module Resolution - TypeScript
In this case, the error would be something like error TS2307: Cannot find module ... Module imports are resolved differently based on whether...
Read more >
ts-loader - npm
ts-loader. This is the TypeScript loader for webpack. ... For details about this functionality, see the module resolution documentation.
Read more >
Webpack | IntelliJ IDEA Documentation - JetBrains
Make sure webpack is listed in the dependencies or devDependencies object of your package.json. If webpack is missing, open the embedded ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found