2

I am using webpack 4.32 for node js. So far the code is properly built when i exclude node_modules, but i want node_modules also to be built in webpack so that when moved to cloud there is no need of installing every time.

Excluded node_modules using, externals : [ nodeExternals() ] . Then background node code builds properly without error and runs on npm start.

If i want to include node_module while build , then i added following code,

module: {
    rules: [{
        test: /\.node$/,
        use: 'native-ext-loader'
      },
      // Support for *.json files.
      {
        test: /\.json$/,
        loader: 'json-loader'
      },
      // all css required in src/app files will be merged in js files
      {
        test: /\.(scss|sass)$/,
        exclude: root('src', 'style'),
        loader: 'raw-loader!postcss-loader!sass-loader'
      },
      {test: /\.js$/,loader: 'script-loader'}
    ]
  }

If i build using npm run build on node server, its building the file properly, but on npm start i get error " [Script Loader] ReferenceError: require is not defined ". I have read the official webpack, which says "requires are not parsed in script loader ". Link : https://webpack.js.org/loaders

Is there any other way we can make webpack parse requires ? Any plugins , other loaders, parsers.

Expected output is node_modules should be built with code, when i run server using npm start then the application must run properly without errors.

Pratima
  • 95
  • 2
  • 9

0 Answers0