0

Silly question perhaps but... I'm building on app with Webpack (on Ubuntu), and I'm trying to require a JS file from another. My app looks something like this:

myapp/
  src/
    vendor/
      facebook.js
    components/
      layout/
        header/
          FacebookButtonComponent.js

And my Webpack configuration has the following in it:

const path = require('path');
const srcPath = path.join(__dirname, '/src');
resolve: {
  extensions: [
    '',
    '.js',
    '.jsx'
  ],
  alias: {
    components: `${ srcPath }/components`,
    vendor: `${ srcPath }/vendor`
  }
}

Then in FacebookComponent.js I'm requiring a facebook.js with:

require('vendor/facebook.js');

However I get the following error:

ERROR in ./src/components/layout/header/FacebookButtonComponent.js Module not found: Error: Cannot resolve 'file' or 'directory' /myapp/src/vendor/facebook.js in /myapp/src/components/layout/header

Maybe I'm going about this the wrong way, but it seems to be building the correct path. How am I supposed to be formatting my path if this isn't correct?

I've also tried ./ which gives:

ERROR in ./src/components/layout/header/FacebookButtonComponent.js Module not found: Error: Cannot resolve 'file' or 'directory' ./vendor/facebook.js in myapp/src/components/layout/header

And ../s to build a relative path, which seems to have no effect:

ERROR in ./src/components/layout/header/FacebookButtonComponent.js Module not found: Error: Cannot resolve 'file' or 'directory' ../vendor/facebook.js in myapp/src/components/layout/header

This question seems somewhat relevant but none of the suggestions there seemed to work (e.g. setting root.)

3
  • can you try to add an empty package.json file inside vendor directory :P Commented Jul 13, 2016 at 15:32
  • any update on this? I'm currently having a similar issue. It seems as though webpack is trying to resolve files from the location of the file that's calling the require rather than the root of the project. Commented Jul 27, 2016 at 23:22
  • If I remember correctly, I think I had some really dumb typo or mismatching class name... I'm not entirely sure. Commented Jul 28, 2016 at 19:15

3 Answers 3

1

You can define resolve.modulesDirectories instead of alias:

resolve: {
  extensions: [
    '',
    '.js',
    '.jsx'
  ],
  modulesDirectories: [
    "node_modules",
    "src"
  ]
}

And require files as it is now:

require('vendor/facebook.js');
Sign up to request clarification or add additional context in comments.

2 Comments

The error output I listed above is the full error output.
Then provide webpack config. Looks like you add vendor folder to ignore path.
0

In your file tree you got

FacebookComponent.js

And your error says

FacebookButtonComponent.js

1 Comment

My mistake, it's actually FacebookButtonComponent. Updated the file structure above. Error is still the same.
0

Just by reading the errors i think require('../../../vendor/facebook.js') should work.


require('vendor/facebook.js');

ERROR in ./src/components/layout/header/FacebookButtonComponent.js Module not found: Error: Cannot resolve 'file' or 'directory' /myapp/src/vendor/facebook.js in /myapp/src/components/layout/header Maybe I'm going about

=> trying to resolve /myapp/src/components/layout/header/myapp/src/vendor/facebook.js


require('./vendor/facebook.js');

ERROR in ./src/components/layout/header/FacebookButtonComponent.js Module not found: Error: Cannot resolve 'file' or 'directory' ./vendor/facebook.js in myapp/src/components/layout/header

=> trying to resolve /myapp/src/components/layout/header/vendor/facebook.js


require('../vendor/facebook.js');

ERROR in ./src/components/layout/header/FacebookButtonComponent.js Module not found: Error: Cannot resolve 'file' or 'directory' ../vendor/facebook.js in myapp/src/components/layout/header

=> trying to resolve myapp/src/components/layout/vendor/facebbok.js

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.