Batman Batman - 5 months ago 38
Javascript Question

How do I load my scripts into webpack

I've used webpack to bundle my npm dependencies in the past, like jquery, lodash, etc.

I'm having trouble figuring out how to bundle dependencies AND my own scripts?


require('slider-component.js'); // my component in app folder


class Slider extends React.Component {
constructor() {
this.state = { data: [] };

componentDidMount() {
let data = [
title: "My Image 1",
url: "",
img: "",
caption: "Something important text"

this.setState({data: data});
}, 5000)

render() {
let data =
let slides =, index)=> {
caption= {slide.caption}
return (
<ul className="slides">

<Slider />,

I get this error:

ERROR in ./app/loader.js Module not found: Error: Cannot resolve
module 'slider-component.js' in
@ ./app/loader.js 10:0-30


You should read this document relating to how webpack resolve modules.

There are three types of module requests:

1) Absolute path require("/home/me/file") require("C:\Home\me\file")

2) Relative path require("../src/file") require("./file")

3) Module path require("module") require("module/lib/file")

Absolute path

We first check if the path points to a directory. For a directory we need to find the main file in this directory. Therefore the main field in the package.json is joined to the path. If there is no package.json or no main field, index is used as filename.

We have an absolute path to a file now. We try to append all extensions (configuration option resolve.extensions). The first existing file is used as result.

Relative path

The context directory is the directory of the resource file that contains the require statement. If there is no resource file the configuration option context is used as context directory. (This can occur for entry points or with loader-generated files).

The relative path is joined to the context directory and the resulting absolute file is resolved according to “Resolving an absolute path”.

Module path

For resolving a module we first gather all search directories for modules from the context directory. This process is similar to the node.js resolving process, but the search directories are configurable with the configuration option resolve.modulesDirectories. In addition to this the directories in the configuration option resolve.root are prepended and directories in the configuration option resolve.fallback are appended.

The module is looked up in each module directory and resolved according to “Resolving an absolute path”. If the first match has no success, the second is tried and so on.

The type of module request you want to use is a module path. So, in your webpack.config.js, you should add/edit the field resolve.modulesDirectories (which is an array of strings) and append the directories that contain your modules.

For example:

var path = require('path');

// ...
resolve: {
    modulesDirectories: [

Also, as the document states, you may use resolve.root or resolve.fallback to specify these directories.