简体   繁体   English

后端API服务器Typescript / Webpack构建:找不到模块:错误:无法解析

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

I get a list of errors when building my TS app. 生成TS应用程序时出现错误列表。 I guess they are Webpack error , but I don't see how to solve them... 我想它们是Webpack错误,但我看不出如何解决它们...

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 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 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后端应用程序后,我找到了解决方案

webpack.config.dev.js 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 })
  ]

});

It seems that you have to mention the target in webpack config file. 似乎您必须在webpack配置文件中提及target

Try this 尝试这个

target: 'node',

Add the above line into your webpack config file. 将以上行添加到您的webpack配置文件中。 The above config option tells webpack not to touch any built-in modules. 上面的config选项告诉webpack不要触摸任何内置模块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 webpack错误-找不到模块:错误:无法解析“ ImageFile” - webpack error - Module not found: Error: Can't resolve 'ImageFile' 找不到模块:错误:无法使用 Webpack 解析“fs” - Module not found: Error: Can't resolve 'fs' Using Webpack Webpack-NodeJS-找不到模块:错误:无法解析“ fs” - Webpack - NodeJS - Module not found: Error: Can't resolve 'fs' 找不到模块:错误:无法在 webpack.common.js 中解析 - Module not found: Error: Can't resolve in webpack.common.js 找不到模块:错误:无法使用 webpack 解析“fs” - Module not found: Error: Can't resolve 'fs' with webpack `npx webpack-dev-server` 时 webpack-dev-server 引发“未找到模块:错误:无法解析” - webpack-dev-server raise “Module not found: Error: Can't resolve” when `npx webpack-dev-server` Angular 构建 - 找不到模块:错误:无法解析“控制台” - Angular build - Module not found: Error: Can't resolve 'console' 如何解决“module not found can't resolve 'Api Client'”错误? - How to solve “module not found can't resolve 'Api Client'” error? ModuleNotFoundError:找不到模块:错误:无法解析“下一个/服务器” - ModuleNotFoundError: Module not found: Error: Can't resolve 'next/server' 找不到主模块中的 WEBPACK5 错误:错误:无法解析“./src” - WEBPACK5 ERROR in main Module not found: Error: Can't resolve './src'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM