Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SyntaxError: unknown: Unexpected token (import) #102

Open
jdferreira opened this issue Mar 24, 2021 · 12 comments
Open

SyntaxError: unknown: Unexpected token (import) #102

jdferreira opened this issue Mar 24, 2021 · 12 comments

Comments

@jdferreira
Copy link

I'm not sure what the correct forum is to ask for help; if you think this is not the place, please direct me towards a better alternative.

I'm starting to learn webpack, following a tutorial which uses html-loader, extract-loader and file-loader. When I apply the extract-loader to an HTML file that depends on a CSS and JS file, webpack seems to be unable to compile the HTML:

Here's a snippet of the error.

SyntaxError: unknown: Unexpected token (3:83)
  1 | // Imports
  2 | import ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ from "../node_modules/html-loader/dist/runtime/getUrl.js";
> 3 | var ___HTML_LOADER_IMPORT_0___ = new URL("./9e9623e0b9c680a171a9f50add100517.css", import.meta.url);
    |                                                                                    ^
  4 | // Module
  5 | var ___HTML_LOADER_REPLACEMENT_0___ = ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___(___HTML_LOADER_IMPORT_0___);

It seems there is a problem with the import keyword.

Is there a way to solve this issue or am I missing understanding somewhere?

Thank you.

@yossi-nagar
Copy link

It happens to me too. Any advice?

@teeramusic
Copy link

I'm having the same problem

@nielslerches
Copy link

I also have this problem.

@teeramusic
Copy link

teeramusic commented May 12, 2021

I thought webpack looked really awesome but because of this I have to go back to grunt lol

@herundina
Copy link

I have the same problem here when I trying extract a html that depends on a CSS and Js file.

Help me, please.

@tlusk
Copy link

tlusk commented Jun 21, 2021

Disable the ES modules syntax in html-loader.
https://webpack.js.org/loaders/html-loader/#esmodule

@alexbainter
Copy link

alexbainter commented Aug 9, 2021

@tlusk Thank you!!

Solution

// in webpack configuration
module: {
  rules: [
     {
       test: /\.html$/, // or a path to your HTML file, etc
       use: [
         'file-loader',
         'extract-loader',
         {
           loader: 'html-loader',
           options: {
             esModule: false,
           }
         },
       ],
     },
  ],
}

Explanation

extract-loader is transforming modules with babel-preset-env (deprecated) which doesn't recognize some of the newer syntax features. I'm guessing #109 will resolve this issue, but for now disabling esModule in html-loader also works.

@CarterPape
Copy link

CarterPape commented Aug 16, 2021

I tried disabling esModule but now get a different error, still coming from extract-loader.

Module build failed (from ./node_modules/extract-loader/lib/extractLoader.js):
NonErrorEmittedError: (Emitted value instead of an instance of Error) TypeError: _insertBySelector2.default.bind is not a function
    at processResult (/.../node_modules/webpack/lib/NormalModule.js:710:12)
    at /.../node_modules/webpack/lib/NormalModule.js:819:5
    at /.../node_modules/loader-runner/lib/LoaderRunner.js:399:11
    at /.../node_modules/loader-runner/lib/LoaderRunner.js:251:18
    at context.callback (/.../node_modules/loader-runner/lib/LoaderRunner.js:124:13)
    at Object.<anonymous> (/.../node_modules/extract-loader/lib/extractLoader.js:40:13)
    at Generator.throw (<anonymous>)
    at step (/.../node_modules/babel-runtime/helpers/asyncToGenerator.js:17:30)
    at /.../node_modules/babel-runtime/helpers/asyncToGenerator.js:30:13

@prionkor
Copy link

I am not using html-loader still get this issue.

@broken
Copy link

broken commented Aug 10, 2022

@prionkor You are using some module that is running the extract-loader. In my case it was the extract-loader after css-loader. Setting the option esModule: false on the css-loader I believe would've worked, but I turned off loaders in general for urls using the option url: false

@richallanb
Copy link

Any update on getting #109 and/or #108 merged or has this loader been abandoned?

@Mario-Eis
Copy link

Disable the ES modules syntax in html-loader. https://webpack.js.org/loaders/html-loader/#esmodule

With url(./whatever.woff) in the file, this raises another error

2023-12-13 14_44_59-fiva-office-app – webpack js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests