Electron-forge: Getting: Unexpected token import

Created on 10 Mar 2018  路  5Comments  路  Source: electron-userland/electron-forge

  • [x] I have read the contribution documentation for this project.
  • [x] I agree to follow the code of conduct that this project follows, as appropriate.
  • [x] I have searched the issue tracker for an issue that matches the one I want to file, without success.

Please describe your issue:
In "dev mode" all works right, but once I run a packaged application it throws me that error:

Uncaught Exception:
/Users/rawnly/Code/ELECTRON/APPS/snahp-tool/out/snahp-tool-darwin-x64/snahp-tool.app/Contents/Resources/app/src/app.js:1
(function (exports, require, module, __filename, __dirname, process, global, Buffer) { return function (exports, require, module, __filename, __dirname) { import { app, BrowserWindow, Menu } from 'electron';
                                                                                                                                                           ^^^^^^

SyntaxError: Unexpected token import
    at createScript (vm.js:74:10)
    at Object.runInThisContext (vm.js:116:10)
    at Module._compile (module.js:533:28)
    at Object.require.extensions.(anonymous function) [as .js] (/Users/rawnly/Code/ELECTRON/APPS/snahp-tool/out/snahp-tool-darwin-x64/snahp-tool.app/Contents/Resources/app/node_modules/electron-compile/lib/require-hook.js:77:14)
    at Module.load (module.js:503:32)
    at tryModuleLoad (module.js:466:12)
    at Function.Module._load (module.js:458:3)
    at Module.require (module.js:513:17)
    at Object.init (/Users/rawnly/Code/ELECTRON/APPS/snahp-tool/out/snahp-tool-darwin-x64/snahp-tool.app/Contents/Resources/app/node_modules/electron-compile/lib/config-parser.js:294:16)
    at Object.<anonymous> (/Users/rawnly/Code/ELECTRON/APPS/snahp-tool/out/snahp-tool-darwin-x64/snahp-tool.app/Contents/Resources/app/es6-shim.js:8:17)

Console output when you run electron-forge with the environment variable DEBUG=electron-forge:*. (Instructions on how to do so here). Please include the stack trace if one exists.


What command line arguments are you passing?


What does your config.forge data in package.json look like?

"config": {
    "forge": {
      "make_targets": {
        "win32": [
          "squirrel"
        ],
        "darwin": [
          "zip"
        ],
        "linux": [
          "deb",
          "rpm"
        ]
      },
      "electronPackagerConfig": {
        "packageManager": "yarn"
      },
      "electronWinstallerConfig": {
        "name": "snahp_tool"
      },
      "electronInstallerDebian": {},
      "electronInstallerRedhat": {},
      "github_repository": {
        "owner": "Rawnly",
        "name": "rawnly/snahp-downloader"
      },
      "windowsStoreConfig": {
        "packageName": "",
        "name": "snahptool"
      }
    }
  }

Please provide either a failing minimal testcase (with a link to the code) or detailed steps to
reproduce your problem. Using electron-forge init is a good starting point, if that is not the
source of your problem.

Most helpful comment

@malept It's easy to reproduce this - I have this issue as well.

The trouble is that you cannot import any ES6 module from node_modules. So if you simply add a folder name _test to your node_modules with anindex.js file containing a one-liner like say import React from 'react' this will not work.

It looks like electron-forge ignores node_modules completely.

Is there any way to modify that somewhere so that it ignores everything except a few selected modules?

All 5 comments

What does your .compilerc look like?

{
  "env": {
    "development": {
      "application/javascript": {
        "presets": [
          [
            "env",
            {
              "targets": {
                "electron": "1.8"
              }
            }
          ],
          "react"
        ],
        "plugins": [
          "transform-async-to-generator"
        ],
        "sourceMaps": "inline"
      }
    },
    "production": {
      "application/javascript": {
        "presets": [
          [
            "env",
            {
              "targets": {
                "electron": "1.8"
              }
            }
          ],
          "react"
        ],
        "plugins": [
          "transform-async-to-generator"
        ],
        "sourceMaps": "none"
      }
    }
  }
}

In order to debug your problem further, we need a minimal testcase to reproduce your problem. Using electron-forge init as a base, could you please create a minimal Electron app that illustrates the issue you described, and post a link to it here?

@malept It's easy to reproduce this - I have this issue as well.

The trouble is that you cannot import any ES6 module from node_modules. So if you simply add a folder name _test to your node_modules with anindex.js file containing a one-liner like say import React from 'react' this will not work.

It looks like electron-forge ignores node_modules completely.

Is there any way to modify that somewhere so that it ignores everything except a few selected modules?

Duplicate #212

Was this page helpful?
0 / 5 - 0 ratings