要求包导致错误

时间:2018-02-19 13:12:33

标签: javascript webpack

每次我捆绑脚本时,都会出现以下错误:

ERROR in ./resources/assets/scripts/main.js
Module not found: Error: Can't resolve 'datatables.net-colreorder-bs4' in '/home/vagrant/sites/laravel/resources/assets/scripts'
 @ ./resources/assets/scripts/main.js 12:23-63

我的webpack配置js:

var path = require('path');
var webpack = require('webpack');

module.exports = {
    entry:  './resources/assets/scripts/main.js',
    output: {
        path: path.resolve(__dirname, './public/js'),
        filename: 'mainBundle.js'
    },
    module: {
        loaders: [
            {
                test: /\.js$/,
                exclude: /node_modules/,
                loader: 'babel-loader',
                query: {
                    presets: ['env']
                }
            }
        ]
    },
    stats: {
        colors: true
    },
    node: {
        fs: 'empty'
    },
    devtool: 'source-map'
};

我的主js文件看起来像这样(没有其他模块导致任何问题)。

var jszip = require('jszip');
var pdfmake = require('pdfmake');
var netBs4 = require('datatables.net-bs4')();
var netButtonsBs4 = require('datatables.net-buttons-bs4')();
var buttonsHtml5 = require('datatables.net-buttons/js/buttons.html5.js')();
var buttonsPrint = require('datatables.net-buttons/js/buttons.print.js')();
var netColreorderBs4 = require('datatables.net-colreorder-bs4')();
var netReponsiveBs4 = require('datatables.net-responsive-bs4')();

我检查了路径,没关系,我也把路径改为绝对路径,但没有任何影响。

包json:

"dependencies": {
        "add": "^2.0.6",
        "babel-core": "^6.26.0",
        "babel-loader": "^7.1.2",
        "chart.js": "^2.7.1",
        "datatables.net-bs4": "^1.10.16",
        "datatables.net-buttons-bs4": "^1.5.1",
        "datatables.net-colreorder-bs4": "^1.4.1",
        "datatables.net-responsive-bs4": "^2.2.1",
        "gulp-changed": "^3.2.0",
        "gulp-debug": "^3.2.0",
        "jquery": "^3.2.1",
        "jquery-ui": "^1.12.1",
        "jszip": "^3.1.5",

由于某种原因,这个问题仍然没有得到解决&datatess.net-colreorder-bs4'在使用npm时将无法找到...我决定将其作为一个随时可用的包加载而不是webpacking ...

2 个答案:

答案 0 :(得分:0)

唯一可能缺少的是resolve,但我不确定webpack默认情况下是否在node_modules中查找:

module.exports = {
  entry:  './resources/assets/scripts/main.js',
  output: {
      path: path.resolve(__dirname, './public/js'),
      filename: 'mainBundle.js'
  },
  resolve: {
    modules: [
      resolve("./node_modules/")
    ]
  },

如果是这种情况会很奇怪,因为找到了其他模块。

答案 1 :(得分:0)

要在客户端模式下使用pdfmake,您需要直接使用构建版本:

window.pdfMake = require('pdfmake/build/pdfmake.js');
var vfs = require('pdfmake/build/vfs_fonts.js');
window.pdfMake.vfs = vfs.pdfMake.vfs;