weixin_39631953
2021-01-11 02:15 阅读 113

external "require('url')":1 Uncaught ReferenceError: require is not defined

When i run npm run start:web to serve the app from the browser i am getting following error. It looks like chrome does not know how to interpret require('url') which is node module. the only place i can find this import is in webpack externals. Can you please point me into right direction?


external "require('url')":1 Uncaught ReferenceError: require is not defined
    at Object.<anonymous> (external "require('url')":1)
    at __webpack_require__ (bootstrap 151bbb78dee4aab80101:54)
    at Object.<anonymous> (client:2)
    at Object.<anonymous> (client?ffdb:204)
    at __webpack_require__ (bootstrap 151bbb78dee4aab80101:54)
    at Object.<anonymous> (polyfills.bundle.js:5881)
    at __webpack_require__ (bootstrap 151bbb78dee4aab80101:54)
    at webpackJsonpCallback (bootstrap 151bbb78dee4aab80101:25)
    at polyfills.bundle.js:1
14:57:33.753 core.es5.js:350 Uncaught reflect-metadata shim is required when using class decorators
</anonymous></anonymous></anonymous></anonymous>

该提问来源于开源项目:maximegris/angular-electron

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

15条回答 默认 最新

  • weixin_39798579 weixin_39798579 2021-01-11 02:15

    Hi You may had imported "URL" node modules even if you are in browser mode. Look at electron.service.ts how node_module dependcies are loaded. (Conditional import with Typescript)

    点赞 评论 复制链接分享
  • weixin_39916355 weixin_39916355 2021-01-11 02:15

    Sorry but where should we import the url and how sorry I dont know if this issue is because of some angular stuff or any library on electron.

    点赞 评论 复制链接分享
  • weixin_39888943 weixin_39888943 2021-01-11 02:15

    facing the same issue here

    点赞 评论 复制链接分享
  • weixin_39631953 weixin_39631953 2021-01-11 02:15

    i am getting this on newly cloned project. Steps to reproduce: 1. clone new project 2. npm install 3. npm run start:web

    Any idea where to start looking? Maybe externals in webpack config?

    点赞 评论 复制链接分享
  • weixin_39635648 weixin_39635648 2021-01-11 02:15

    I found it lives in pollyfills.bundle.js and the code

    
    /***/ "fs":
    /***/ (function(module, exports) {
    
    if(typeof require('fs') === 'undefined') {var e = new Error("Cannot find module \"require('fs')\""); e.code = 'MODULE_NOT_FOUND'; throw e;}
    module.exports = require('fs');
    
    /***/ }),
    
    /***/ "url":
    /***/ (function(module, exports) {
    
    module.exports = require('url');
    
    /***/ })
    

    As you can see, when importing the url module, it doesn't check if it exists before. So why?

    点赞 评论 复制链接分享
  • weixin_39993301 weixin_39993301 2021-01-11 02:15

    The same issue I can not understand how to solve. Is it something in webpack configuration?

    点赞 评论 复制链接分享
  • weixin_39798579 weixin_39798579 2021-01-11 02:15

    What version of this repo do you use ? (I suggest to migrate to v3.x. There were a lot of refactoring) Do you import node/electron modules in your code ?

    点赞 评论 复制链接分享
  • weixin_39639174 weixin_39639174 2021-01-11 02:15

    I have the same issue, it seems like I had ejected the project. I got this issue in the browser with start:web. It runs well on Electron.

    点赞 评论 复制链接分享
  • weixin_39798579 weixin_39798579 2021-01-11 02:15

    All node modules import are done in electron.service.ts ? And all use of node modules are wrapped by an if(this.electronService.isElectron()) to be sure you are using it in an electron app and not a webapp?

    点赞 评论 复制链接分享
  • weixin_39959505 weixin_39959505 2021-01-11 02:15

    I managed to solve this by adding target: 'web' in my webpack.config.js and thereafter restarting my application with npm start!

    点赞 评论 复制链接分享
  • weixin_39798579 weixin_39798579 2021-01-11 02:15

    Please have a look at the last version (4.1.0) [especially commits 165c50a1e964718cbde9881db230a134194438eb 1c403a80fad3be355ed99d924f4d8601a34aa117 50657d01265ec5c9dddc957e3d3e5288b1220753] It should makes node modules (and other native libs) working and remove require errors.

    点赞 评论 复制链接分享
  • weixin_39849418 weixin_39849418 2021-01-11 02:15

    this is still happening

    点赞 评论 复制链接分享
  • weixin_39753857 weixin_39753857 2021-01-11 02:15

    I had this problem happen recently when upgrading from Electron 4 to Electron 5.

    Solution: https://stackoverflow.com/questions/55093700/electron-5-0-0-uncaught-referenceerror-require-is-not-defined

    It turns out, nodeIntegration was true by default in previous electron versions, but false by default in 5.0.0.

    Setting nodeIntegration to true may fix your upgrade from Electron 4 to 5:

    
    new BrowserWindow({
        webPreferences: {
          nodeIntegration: true,
    
    点赞 评论 复制链接分享
  • weixin_39630999 weixin_39630999 2021-01-11 02:15

    doesn't work for me =( image

    点赞 评论 复制链接分享
  • weixin_39674978 weixin_39674978 2021-01-11 02:15

    Great project. Thank you. This is my 3rd project with it.

    I just cloned angular-electron to start a new project (3rd). The same error with require came up with npm start for only http://localhost:4200/ (as in my 2nd project which I never spent time to figure out. Now I did). npm start invokes both web & electron app. Which is the ideal scenario.

    Also, running ng:serve produces the require error for only http://localhost:4200/.

    The solution:

    • Do not use ng:serve
    • Do use: ng:serve:web

    or change npm start to:

    "start": "npm-run-all -p ng:serve:web electron:serve"

    I suggest removing reference to ng:serve.

    点赞 评论 复制链接分享

相关推荐