简体   繁体   English

Webpack - 将节点模块放入 Bundle 并加载到 html 文件中

[英]Webpack - Getting node modules Into Bundle and loading into html file

I am trying to use node_modules in the browser via WebPack. I've read the tutorial and beginning steps but am stuck.我正在尝试通过 WebPack 在浏览器中使用 node_modules。我已经阅读了教程和开始步骤,但卡住了。

I have used webpack to generate bundle.js with the webpack config below and upon going to my index.html in Chrome browser I get the error:我已经使用 webpack 通过下面的 webpack 配置生成 bundle.js,然后在 Chrome 浏览器中访问我的 index.html 我收到错误:

Uncaught ReferenceError: require is not defined at Object.<anonymous> (bundle.js:205)

What additional steps do I have to do to get the browser to recongnize require?我还需要执行哪些额外步骤才能让浏览器识别需求?

index.html index.html

<script src="bundle.js"></script>

<button onclick="EntryPoint.check()">Check</button>

index.js索引.js

const SpellChecker = require('spellchecker');

module.exports = {
      check: function() {
            alert(SpellChecker.isMisspelled('keng'));
      }
};

package.json package.json

{
  "name": "browser-spelling",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "dependencies": {
    "node-loader": "^0.6.0",
    "spellchecker": "^3.3.1",
    "webpack": "^2.2.1"
  }
}

webpack.config.js webpack.config.js

module.exports = {
    entry: './index.js',
    target: 'node',
    output: {
        path: './',
        filename: 'bundle.js',
        libraryTarget: 'var',
        library: 'EntryPoint'
    },
    module: {
        loaders: [
            {
                test: /\.node$/,
                loader: 'node-loader'
            },
            {
                test: /\.js$/,
                exclude: /node_modules/,
                loader: 'babel-loader',
                query: {
                    presets: ['es2015']
                }
            }
        ]
    }
};

In your webpack.config.js you specified that you want to build this bundle for Node.js: 在您的webpack.config.js您指定要为Node.js构建此捆绑包:

target: 'node',

And webpack decided to keep require calls, because Node.js supports them. 并且webpack决定保持require调用,因为Node.js支持它们。 If you want to run it in a browser, you should use target: 'web' instead. 如果要在浏览器中运行它,则应使用target: 'web'

In the new "webpack": "^5.75.0", Required has been changed to rules在新的 "webpack": "^5.75.0", Required 已经改为规则

webpack.config.js webpack.config.js

const path = require("path");
var webpack = require("webpack");
module.exports = {
  entry: "./index.js",
  target: "node",
  output: {
    filename: "bundle.js",
    path: path.resolve(__dirname, "dist"),
  },
  module: {
    rules: [
     {
        test: /\.node$/,
        use: "node-loader",
      },
      {
        test: /\.js$/,
        exclude: /node_modules/,
      },
    ],
  },
};

package.json package.json

{
  "name": "arrybsc",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
  "bootstrap": "^5.2.3",
  "lodash": "^4.17.21"
 },
  "devDependencies": {
    "webpack": "^5.75.0",
    "webpack-cli": "^5.0.1"
  }
}

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

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