5

I need to load a JS file as a string so I can run some analysis on it. I am trying to use raw-loader with Webpack (2.2.0).

I get: Cannot find module 'raw-loader!../

I've tried (yes, the path is correct):

let app = require('raw-loader!../../app.js').default;

let app = require('!!raw-loader!../../app.js').default;

I've even tried it without inline. Raw-loader doesn't get engaged, it just tried to load the JS file normally:

module.exports = {
  module: {
    rules: [
      {
        test: /\app.js$/i,
        use: 'raw-loader',
        loader: 'raw-loader',
      }
    ]
  }
}

raw-loader is in my package.json for the project. It is present in my node modules. I've blown-away my node_modules and have reinstalled. I've looked at many solutions and nothing seems to point to a fix.

Diodeus - James MacFarlane
  • 112,730
  • 33
  • 157
  • 176

3 Answers3

2

You have an error in your regexp pattern.

You are using \a which matches the bell character (ASCII 7)

And . matches any character, you need to escape it.

Moreover, using both use and loader is misleading. You should use only one - see this answer: When do I use 'use' and 'loader' in Webpack 2 module.rules?

Try to use:

module.exports = {
  module: {
    rules: [
      {
        test: /app\.js$/i,
        loader: 'raw-loader'
      }
    ]
  }
}
lavor
  • 1,787
  • 1
  • 14
  • 21
0

Try adding resolve module extensions, resolve loaders

https://webpack.js.org/configuration/resolve/#resolveloader

https://webpack.js.org/configuration/resolve/#resolveloadermoduleextensions

chans
  • 5,104
  • 16
  • 43
0

Firstly I would update webpack. The version that you are using is, currently, 2 major versions behind the latest release. Maybe the error messages have been improved! Also, you can see more info about the error running webpack --display-error-details.

Davide Patti
  • 3,391
  • 2
  • 18
  • 20