简体   繁体   English

使用Angular 2 / webpack“未捕获的ReferenceError:require未定义”

[英]“Uncaught ReferenceError: require is not defined” with Angular 2/webpack

I am working an HTML template from a graphic design company into my Angular 2 project using node and webpack. 我正在使用node和webpack将图形设计公司的HTML模板工作到我的Angular 2项目中。

The HTML pulls in various scripts like this: HTML提取各种脚本,如下所示:

<script src="js/jquery.icheck.min.js"></script>
<script src="js/waypoints.min.js"></script>

so I am requiring them in my component.ts: 所以我在我的组件中要求它们:

var icheckJs = require('../js/jquery.icheck.min');
var waypointsJs = require('../js/waypoints.min');

There are several other scripts too and some SASS which appears to be working correctly. 还有其他一些脚本和一些SASS似乎正常工作。

webpack is happy and build it all and an 'npm start' is successful too. webpack很高兴并且构建了一切,'npm start'也很成功。 However, when it reaches the browser, I get this complaint: 但是,当它到达浏览器时,我得到了这个投诉:

Uncaught ReferenceError: require is not defined node_modules/angular2/platform/browser.js:1 Uncaught ReferenceError: require is not defined

which is actually thrown by this line from url.js: 这个行实际上是从url.js抛出的:

var punycode = require('punycode');

Is this a CommonJs require? 这是CommonJs要求吗? I hadn't used this in web development before a few weeks ago so I'm still untangling the various requires from webback, CommonJs et at. 几个星期前我还没有在Web开发中使用过这个,所以我还在解决webback的各种需求,CommonJs等。

An extract from my webpack.config.js for the .js loader looks like this: 我的webpack.config.js中.js加载器的摘录如下所示:

{ test: /\.js$/, loader: 'script' }

How do I work around this error? 我该如何解决此错误?

WebPack can do this alone. WebPack可以单独完成。 You need to make sure you load the initial chunk first using a script src tag. 您需要确保首先使用脚本src标记加载初始块。 It will typically be the value of the entry: key in the WebPack config with -bundle appended. 它通常是条目的值:附加-bundle的WebPack配置中的键。 If you're not doing explicit chunking, your entry chunk should be both an initial and entry chunk and have the WebPack runtime in it. 如果您没有进行显式分块,那么您的条目块应该是初始块和入口块,并且其中包含WebPack运行时。 The WebPack runtime contains and loads the require function before your code runs. WebPack运行时包含并在代码运行之前加载require函数。

Your components or whatever you're requiring need to be required from the entry file since your scripts will start there. 因为您的脚本将从那里开始,因此需要从条目文件中要求您的组件或您需要的任何内容。 Basically, if you're not explicitly chunking, the entry point JS file is the only one you can include with script src. 基本上,如果您没有明确分块,那么入口点JS文件是您可以在脚本src中包含的唯一文件。 Everything else needs to be required from it. 其他所有东西都需要它。 What you include will typically have bundle in the JS filename. 您包含的内容通常包含JS文件名中的包。 By default, it should be main-bundle.js. 默认情况下,它应该是main-bundle.js。

You can do this in one line assumed that you have 你可以假设你有一行这样做

  • the bundle in dist/bundle.js dist/bundle.js的bundle
  • the source file client code that will render the page in the browser in client/client.js 源文件客户端代码,它将在client/client.js中的浏览器中呈现页面

    webpack && webpack ./client/client.js dist/bundle.js \\ && webpack-dev-server --progress --color

You need to run webpack again since if some sources in the library change you will get the last changes then in the dist/bundle.js package (of course you can add like a grunt file watch task for this). 您需要再次运行webpack ,因为如果库中的某些源发生更改,您将在dist/bundle.js包中获得最后的更改(当然,您可以为此添加类似grunt文件监视任务)。 webpack-dev-server will run the server then. webpack-dev-server server然后运行服务器。

For anyone that is looking for an answer but the above doesn't work: 对于正在寻找答案的人,但上述方法不起作用:

Short

Add or Replace current target in webpack.config.js to target: 'web' webpack.config.js当前target添加或替换为target: 'web'

A bit longer 有点长

Webpack has different targets , if you've experimented with this and changed your target to node it will use 'require' to load chuncks. Webpack有不同的目标 ,如果您已经尝试过这个并将目标更改为node ,它将使用'require'来加载chuncks。 The best thing is to make your target (or to add) target: 'web' in your webpack.config.js . 最好的办法是在webpack.config.js制作目标(或添加) target: 'web' This is the default target and loads your chuncks in a way the browser can handle. 这是默认目标,并以浏览器可以处理的方式加载您的组块。

I eventually found this solution here . 我最终在这里找到了解决方案。

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

相关问题 webpack:未捕获的 ReferenceError:未定义要求 - webpack : Uncaught ReferenceError: require is not defined Uncaught ReferenceError:在Angular 2 Webpack全局库安装中未定义require - Uncaught ReferenceError: require is not defined on Angular 2 webpack global library installation 未捕获的ReferenceError:角度2中未定义require - Uncaught ReferenceError: require is not defined in angular 2 未捕获的ReferenceError:require未定义react + webpack - Uncaught ReferenceError: require is not defined react + webpack React 和 Webpack:“未捕获的 ReferenceError:需要未定义” - React and Webpack: “Uncaught ReferenceError: require is not defined” 未捕获的 ReferenceError:未在 webpack 设置上定义要求 - Uncaught ReferenceError: require is not defined on webpack setup 未捕获的ReferenceError:未定义require-Webpack2 - Uncaught ReferenceError: require is not defined - Webpack2 未捕获的ReferenceError:未定义require Webpack + AngularJS - Uncaught ReferenceError: require is not defined Webpack + AngularJS 未捕获的ReferenceError:require未定义webpack与电报 - Uncaught ReferenceError: require is not defined webpack with telegram 带有Webpack的ReactJS:未捕获的ReferenceError:需求未定义 - ReactJS with Webpack: Uncaught ReferenceError: require is not defined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM