Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webpack - module not found even though module exists

This is a branch off of my previous question and applied suggestions. But I am still having major issues.

I now have my babel transpiler, along with a .babelrc file in place. My import code to import my module looks like this:

var init = require('./app/js/modules/toc');
init();

However I'm getting this:

ERROR in ./app/js/script.js
Module not found: Error: Cannot resolve 'file' or 'directory' ./app/js/modules/toc in /projects/project-root/app/js
 @ ./app/js/script.js 1:11-42

Webpack config:

var debug = process.env.NODE_ENV !== "production";
var webpack = require('webpack');

module.exports = {
  context: __dirname,
  devtool: debug ? "inline-sourcemap" : null,
  entry: "./app/js/script.js",
  module: {
  rules: [{
      test: /\.js$/,
      use: 'babel-loader'
    }]
  },
  output: {
    path: __dirname + "public/javascripts",
    filename: "scripts.min.js"
  },
  plugins: debug ? [] : [
    new webpack.optimize.DedupePlugin(),
    new webpack.optimize.OccurenceOrderPlugin(),
    new webpack.optimize.UglifyJsPlugin({ mangle: false, sourcemap: false }),
  ],
};

.babelrc

{
  "presets": ["es2015"]
}

Gulptask

//scripts task, also "Uglifies" JS
gulp.task('scripts', function() {
    gulp.src('app/js/script.js')
    .pipe(webpack(require('./webpack.config.js')))
    .pipe(gulp.dest('public/javascripts'))
    .pipe(livereload());
});

I'm totally lost...what am I doing wrong?

For my import code I also tried:

import {toc} from './modules/toc'
toc();

UPDATE: As recommended I needed to add resolve to my config. It looks like this now:

var debug = process.env.NODE_ENV !== "production";
var webpack = require('webpack');

module.exports = {
  context: __dirname,
  devtool: debug ? "inline-sourcemap" : null,
  entry: "./app/js/script.js",
  resolve: {
    extensions: ['.js']
  },
  module: {
  rules: [{
      test: /\.js$/,
      use: 'babel-loader'
    }]
  },
  output: {
    path: __dirname + "public/javascripts",
    filename: "scripts.min.js"
  },
  plugins: debug ? [] : [
    new webpack.optimize.DedupePlugin(),
    new webpack.optimize.OccurenceOrderPlugin(),
    new webpack.optimize.UglifyJsPlugin({ mangle: false, sourcemap: false }),
  ],
};

Sadly I still get:

ERROR in Entry module not found: Error: Cannot resolve 'file' or 'directory' ./app/js/script.js in /projects/project-root

Does my file structure need to change?

like image 755
kawnah Avatar asked Oct 20 '17 13:10

kawnah


People also ask

Can not find module webpack JS?

To solve the "Cannot find module 'webpack'" error, make sure to install webpack globally by running the npm i -g webpack command and create a symbolic link from the globally-installed package to node_modules by running the npm link webpack command. Copied! Once you run the two commands, the error should be resolved.

Where is webpack config JS?

To answer your specific question, the webpack configuration is stored wherever your global node_modules are installed; on Windows this is typically %AppData%\Roaming\npm\node_modules\powerbi-visuals-tools\lib\webpack. config. js.


2 Answers

Whenever you import/require a module without specifying a file extension, you need to tell webpack how to resolve it. This is done by the resolve section inside the webpack config.

resolve: {
  extensions: ['.js'] // add your other extensions here
}

As a rule of thumb: whenever webpack complains about not resolving a module, the answer probably lies in the resolve config.

Let me know about any further questions and if this works.

EDIT

resolve directly to the root level of your config:

// webpack.config.js
module.export = {
  entry: '...',
  // ...
  resolve: {
    extensions: ['.js']
  }
  // ...
};
like image 91
Rico Herwig Avatar answered Nov 15 '22 04:11

Rico Herwig


You are specifying an entry point in your webpack config AND in gulp. Remove the entry property in your webpack config.

If you specify it twice, the gulp config will tell webpack to get the file in ./app/js/script.jsand then webpack in ./app/js/script.js which will result in a path like ./app/js/app/js/script.js.

Keep us posted if you fixed it. =)

like image 20
ChrisR Avatar answered Nov 15 '22 04:11

ChrisR