Skip to content

Getting "TypeError: t is not a function" when trying to replicate script import issue #209

Open
@SMen1

Description

@SMen1

I was getting the following error when trying to use vue.js composables and import a script js file.

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

I have tried to replicate the solutions here:
#44 and here
#14 (comment)

Specifically this solution:

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("/test.js") ? { content: content, type: ".mjs" } : content;
return content;
},

Where the test.js file is :

export const user = {
userName: "root",
password: "zfs1111"
}

is giving me the error: TypeError: t is not a function in the console. I get the same error trying to import a non-test js file.

Is this an old solution that is no longer viable? What is the new solution if yes?

using Vue3.js

Versions

  • Browser (name & version):
  • vue3-sfc-loader:

Additional context

Activity

FranckFreiburger

FranckFreiburger commented on Nov 22, 2024

@FranckFreiburger
Owner

hello SMen1,

yes, the solution you are talking about is quite old
{ content: content, type: ".mjs" } is not valid any more.

In order to handle binary files (ex. png) I managed to change 'content' property into
getContentData: (asBinary: Boolean) => Promise<ContentData>

Now, your option.getFile function should look like this:

      async getFile(url) {
        
        const res = await fetch(url);
        if ( !res.ok )
          throw Object.assign(new Error(res.statusText + ' ' + url), { res });

        // make test.js be treated as an mjs file
        if ( url.endsWith("/test.js") ) {
          return {
              getContentData: async (asBinary) => { // asBinary is unused here, we know it is a text file
                
                return await res.text();
              },
              type: ".mjs",
          }          
        }

        return {
          getContentData: asBinary => asBinary ? res.arrayBuffer() : res.text(),
        }
      },
added
questionFurther information is requested
and removed
bugSomething isn't working
on Nov 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @SMen1@FranckFreiburger

        Issue actions

          Getting "TypeError: t is not a function" when trying to replicate script import issue · Issue #209 · FranckFreiburger/vue3-sfc-loader