user762579
user762579

Reputation:

Backend API server Typescript/Webpack build : Module not found: Error: Can't resolve

I get a list of errors when building my TS app. I guess they are Webpack error , but I don't see how to solve them...

ERROR in ./~/errorhandler/index.js
Module not found: Error: Can't resolve 'fs' in '/Users/yves/Developments/IDEALCOMS/project/api_cockpit/node_modules/errorhandler'

ERROR in ./~/serve-favicon/index.js
Module not found: Error: Can't resolve 'fs' in '/Users/yves/Developments/IDEALCOMS/project/api_cockpit/node_modules/serve-favicon'

ERROR in ./~/etag/index.js
Module not found: Error: Can't resolve 'fs' in '/Users/yves/Developments/IDEALCOMS/project/api_cockpit/node_modules/etag'

ERROR in ./~/express/lib/request.js
Module not found: Error: Can't resolve 'net' in '/Users/yves/Developments/IDEALCOMS/project/api_cockpit/node_modules/express/lib'

ERROR in ./~/express/lib/view.js
Module not found: Error: Can't resolve 'fs' in '/Users/yves/Developments/IDEALCOMS/project/api_cockpit/node_modules/express/lib'

ERROR in ./~/send/index.js
Module not found: Error: Can't resolve 'fs' in '/Users/yves/Developments/IDEALCOMS/project/api_cockpit/node_modules/send'

ERROR in ./~/destroy/index.js
Module not found: Error: Can't resolve 'fs' in '/Users/yves/Developments/IDEALCOMS/project/api_cockpit/node_modules/destroy'

ERROR in ./~/mime/mime.js
Module not found: Error: Can't resolve 'fs' in '/Users/yves/Developments/IDEALCOMS/project/api_cockpit/node_modules/mime'

webpack.config.common.js

var webpack = require('webpack');

module.exports = {
  entry: {
    'app': './src/index.ts'
  },

  resolve: {
    extensions: ['.js', '.ts']
  },

  module: {
    loaders: [
      {
        test: /\.ts$/,
        loaders: 'ts-loader'
      },
      {
        test: /\.html$/,
        loader: 'html-loader'
      },
      {
        test: /\.css$/,
        loader: 'raw-loader'
      }
    ]
  },
  plugins: []
};

webpack.config.dev.js

const path = require('path');

var webpack = require('webpack');
var webpackMerge = require('webpack-merge');
var commonConfig = require('./webpack.config.common.js');

var env = require('./env.json');

module.exports = webpackMerge(commonConfig, {

  devtool: 'cheap-module-eval-source-map',

  output: {
    path: path.resolve(__dirname, 'dist'),
    publicPath: "/js/app/",
    filename: 'bundle.js',
    chunkFilename: '[id].chunk.js'
  },

  plugins: []

});

UPDATE ===========================

I found a solution after reading the blog post : Backend Apps with Webpack

webpack.config.dev.js

const path = require('path');
var fs = require('fs');

var webpack = require('webpack');
var webpackMerge = require('webpack-merge');
var commonConfig = require('./webpack.config.common.js');

var nodeModules = {};
fs.readdirSync('node_modules')
  .filter(function(x) {
    return ['.bin'].indexOf(x) === -1;
  })
  .forEach(function(mod) {
    nodeModules[mod] = 'commonjs ' + mod;
  });

module.exports = webpackMerge(commonConfig, {

  devtool: 'cheap-module-eval-source-map',

  output: {
    path: path.resolve(__dirname, 'build'),
    filename: 'backend.js',
    chunkFilename: '[id].chunk.js'
  },
  externals: nodeModules,

  plugins: [
    new webpack.IgnorePlugin(/\.(css|less)$/),
    new webpack.BannerPlugin('require("source-map-support").install();',
      { raw: true, entryOnly: false })
  ]

});

Upvotes: 0

Views: 2022

Answers (1)

Thaadikkaaran
Thaadikkaaran

Reputation: 5226

It seems that you have to mention the target in webpack config file.

Try this

target: 'node',

Add the above line into your webpack config file. The above config option tells webpack not to touch any built-in modules.

Upvotes: 4

Related Questions