最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - 'Unexpected identifier' when trying to import modules in Chrome extension - Stack Overflow

programmeradmin1浏览0评论

I'm developing a Chrome extension that will make use of some background scripts. I thought it would be interesting to make use of modules since Google recently added native support for them.

However, I'm getting a 'Uncaught SyntaxError: Unexpected identifier' error when I'm attemtping to import a module. The errors points to the line of code where the import is written. Here's an example:

In main.js:

import test from './test.js';

In test.js:

export default function test () {
  console.log('this is a test.');
}

I've tried various other formats, but none of them works. Interestingly, Chrome's newest import('file.js') function works just fine. However, I'm looking for a way to import modules without using promises.

Am I doing something wrong, or am I just not supposed to make use of modules in Chrome Extensions?

Thanks in advance.

I'm developing a Chrome extension that will make use of some background scripts. I thought it would be interesting to make use of modules since Google recently added native support for them.

However, I'm getting a 'Uncaught SyntaxError: Unexpected identifier' error when I'm attemtping to import a module. The errors points to the line of code where the import is written. Here's an example:

In main.js:

import test from './test.js';

In test.js:

export default function test () {
  console.log('this is a test.');
}

I've tried various other formats, but none of them works. Interestingly, Chrome's newest import('file.js') function works just fine. However, I'm looking for a way to import modules without using promises.

Am I doing something wrong, or am I just not supposed to make use of modules in Chrome Extensions?

Thanks in advance.

Share Improve this question asked Dec 31, 2017 at 5:26 Kim Nedergaard ClausenKim Nedergaard Clausen 2321 gold badge3 silver badges9 bronze badges 1
  • 1 Possible duplicate of ES6 module Import giving "Uncaught SyntaxError: Unexpected identifier" – Kaibo Commented Nov 1, 2019 at 12:58
Add a comment  | 

3 Answers 3

Reset to default 9

If you are getting an error shown below while using import (ES6 module).

You need to inform the browser by using the type="module" in the script tag you are using in the HTML file. Use this to link you js file in HTML

<script type="module" src="./main.js"></script>

You should add type='module' to the main.js import line

look at this ES6 module Import giving "Uncaught SyntaxError: Unexpected identifier"

Manifest V2 Chrome extensions do not natively support ES modules. In order to use them in a Manifest V2 extension, you’ll need to use a bundler like Webpack.

Manifest V3 Chrome extensions have support for ES modules in background service workers. The Chrome documentation explains how to enable ES modules by adding a field to manifest.json:

You can optionally specify an extra field of "type": "module" to include the service worker as an ES Module, which allows you to import further code. See ES modules in service workers for more information. For example:

  "background": {
    "service_worker": "background.js",
    "type": "module"
  }

ES modules are supported in service workers since Chrome 91, though I’m not sure if they were introduced for Manifest V3 service workers at the same time.

发布评论

评论列表(0)

  1. 暂无评论