简体   繁体   English

webpack无法导入npm软件包

[英]webpack failing to import npm package

I've got a very simple webpack config 我有一个非常简单的webpack配置

const HtmlWebpackPlugin = require("html-webpack-plugin");

module.exports = {
  entry: "./scripts/app.ts",
  output: {
    filename: "./www/scripts/appBundle.js",
  },
  resolve: {
    extensions: [".ts", ".js"]
  },
  devServer: {
    contentBase: './www'
  },
  module: {
    rules: [
      {
        test: /\.tsx?$/,
        use: [
          {
            loader: "ts-loader",
            options: {
              configFile: "scripts/tsconfig.json"
            }
          }
        ]
      }
    ]
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: './www/index.ejs'
    })
  ],
  devtool: "source-map"
};

I'm importing an npm package (nouislider), build works fine but when I run the app I get an error (noUiSlider is undefined). 我正在导入一个npm包(nouislider),构建可以正常运行,但是当我运行该应用程序时出现错误(noUiSlider未定义)。

I've set up a debug on chrome to try and find out what's happening - when I inspect the import I see an error - ReferenceError: noUiSlider is not defined at eval but when I inspect the actual execution line the debugger does recognize the import. 我在chrome上设置了调试程序,以尝试找出发生的情况-当我检查导入时,我看到一个错误ReferenceError: noUiSlider is not defined at eval但是当我检查实际的执行行时,调试器ReferenceError: noUiSlider is not defined at eval识别出该导入。

The execution still fails as the import is not recognized. 由于无法识别导入,执行仍然失败。

So far I've tried to add the babel-loader thinking it might be failing there, but I'm getting the same error. 到目前为止,我已经尝试添加babel-loader,以为它可能在那里失败,但是我遇到了同样的错误。

Any idea what I'm missing? 知道我缺少什么吗?

Just a guess but you might have not installed this npm module? 只是一个猜测,但您可能尚未安装此npm模块?

could you try running: 你可以尝试运行:

npm install html-webpack-plugin --save-dev

try adding something similar to 'babel-present-env' dependency to compile ES2015+ down to ES5. 尝试添加类似于“ babel-present-env”的依赖项,以将ES2015 +编译为ES5。 Looking at your config could you try changing your ts rule to this: 查看您的配置,您可以尝试将ts规则更改为此:

{
  test: /\.ts(x?)$/,
  exclude: /node_modules/,
  use: [
    {
      loader: 'babel-loader',
      options: {
        presets:['env']
      }
    },
    {
      loader: 'ts-loader'
    }
  ]
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM