question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is it possible to import from an ES6 module in a component (SFC) ?

See original GitHub issue

Hi, First I wan’t to thank you for this librairy which is going to be very useful for me. I can’t manage to import from a module inside a script tag of a .vue file For example :

<template>
  <div class="test">Test</div>
</template>

<script>
import { label } from "./../tools.js";
export default {
  name: "appWorld",
  computed: {
    ...Vuex.mapState(["count"]),
  },
  mounted() {
    console.log(`Component ${this.$options.name} mounted - ${label}`);
  },
};
</script>

<style scoped>
div.test {
  color: red;
}
</style>

The line _import { label } from "./../tools.js";_

raises the following error.

_'import' and 'export' may appear only with 'sourceType: "module"' (2:0)_

I tried adding type="module" to the script tag but it didn’t work either.

Is it possible to do that ? How ? Thank you for your answer

Raphael

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
raphael-bressoncommented, Apr 12, 2021

Ok great, thank you for your answer ! I chose option 2 which worked smoothly

    async getFile(url) {
      const res = await fetch(url);
      if (!res.ok)
        throw Object.assign(new Error(url + " " + res.statusText), { res });
      let content = await res.text();
      content = url.endsWith("/tools.js")
        ? { content: content, type: ".mjs" } // `type` since v0.7.0 and `extname` before
        : content;
      return content;
    },
1reaction
fushengruomengzhangcommented, Apr 12, 2021

return {content: content, type: “.mjs”}

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cannot Import ES6 module into Vue single file component
js, single file components (SFC). I built my project with the Vue CLI via the webpack-simple template. I get an error "TypeError: Cannot...
Read more >
ES6 Modules and How to Use Import and Export in JavaScript
You can export members one by one. What's not exported won't be available directly outside the module: export const myNumbers = [ ...
Read more >
JavaScript/ES Modules and Imports - CodePen Blog
The Vue SFC Editor (Single File Component) not only supports the usage of ES Modules within the JavaScript <script> area, CodePen processes your...
Read more >
How to use ES6 import syntax in Node.js - DEV Community ‍ ‍
An ES6 import syntax allows importing modules exported from a different JavaScript file. It is a common pattern to use modules across React ......
Read more >
16. Modules - Exploring JS
The imports of an ES6 module are read-only views on the exported entities. That means that the connections to variables declared inside module...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found