Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Webpack Can';在网页包中的module.exports之后找不到变量_Webpack - Fatal编程技术网

Webpack Can';在网页包中的module.exports之后找不到变量

Webpack Can';在网页包中的module.exports之后找不到变量,webpack,Webpack,我最近开始使用Webpack,遇到了这个问题。我有一个“copyright.js”文件,其中我将函数导出为模块。在'index.js'文件中,我需要'copyright.js'。在我的'webpack.config.js'中,我输出一个'app.bundle.js'文件,我可以在生成后看到'getCopyright()'函数 如何调用html脚本标记中的函数以便返回值?到目前为止,我得到了“找不到变量getCopyright”或者当我调用“copyright.getCopyright();”时我

我最近开始使用Webpack,遇到了这个问题。我有一个“copyright.js”文件,其中我将函数导出为模块。在'index.js'文件中,我需要'copyright.js'。在我的'webpack.config.js'中,我输出一个'app.bundle.js'文件,我可以在生成后看到'getCopyright()'函数

如何调用html脚本标记中的函数以便返回值?到目前为止,我得到了“找不到变量getCopyright”或者当我调用“copyright.getCopyright();”时我得到“找不到可变版权”

多谢各位

版权所有.js

webpackconfig.js

index.js

index.html


获取版权();

网页包中的所有内容都是一个模块。甚至您的非模块化代码也会打包到一个模块中
var copyright
(index.js)不在全局范围内,因此当您运行脚本标记时,无法访问版权

你当然可以:

window.copyright = require('./copyright');
但接下来我们直接回到使用全局对象。(当然,如果您正在编写一些其他人应该包含的web库,那么您迟早要向浏览器公开一些内容。)

有了webpack,如果您绝对愿意,可以在html中内联脚本,但好的解决方案是从模块而不是从脚本标记(使用全局环境)运行您的东西

因此,请从index.js运行代码(这也是您将其声明为入口点的原因):


(是的,这确实使document.write()有点事与愿违。)

谢谢ippi的回答和解释!非常有用。
module.exports = {
    entry: {
        app: './src/index.js',
        filename1: './src/filename1.js',
        filename2: './src/filename2.css'
    },
    output: {
      path: path.resolve(__dirname, './dist'),
      filename: '[name].bundle.js'
    },
var copyright = require('./copyright');
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <script type="text/javascript" src="app.bundle.js"></script>
</head>
  <body>
    <div class="foot">
      <script>getCopyright();</script>
    </div>
  </body>
</html>
window.copyright = require('./copyright');
let copyright = require('./copyright');
copyright.getCopyright();