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

default export doesn't work. "TypeError: xxx is not a function" #54

Open
colecrouter opened this issue Apr 30, 2022 · 19 comments
Open

default export doesn't work. "TypeError: xxx is not a function" #54

colecrouter opened this issue Apr 30, 2022 · 19 comments

Comments

@colecrouter
Copy link

First off, thank you for making this!

I noticed in the installation section, your vite.config.js reads:

import comlink from 'vite-plugin-comlink'

In practice, I'm only able to get it to work with:

import { comlink } from 'vite-plugin-comlink'

Is this the intended behaviour?

@mathe42
Copy link
Owner

mathe42 commented May 1, 2022

Yes your right will fix the code

@mathe42 mathe42 closed this as completed in a9318b6 May 1, 2022
@mathe42
Copy link
Owner

mathe42 commented May 1, 2022

Released a fix as 3.0.1. Thanks for the report!

@ivancuric
Copy link

Still doesn't work. If importing the default export, I get the following error:

TypeError: comlink is not a function

@mathe42
Copy link
Owner

mathe42 commented May 24, 2022

Are you sure you are on the latest version?

@ivancuric
Copy link

"vite-plugin-comlink": "^3.0.2"

@mathe42
Copy link
Owner

mathe42 commented May 24, 2022

It works properly see https://github.com/mathe42/comlink-test please change to "3.0.2" and not "^3.0.2".

I need a full reproduction to fix that.

@ivancuric
Copy link

Here's my repro:
https://github.com/ivancuric/vite-workers/tree/repro-broken-comlink

failed to load config from /Users/ivan/Dev/vite-workers/vite.config.js
error when starting dev server:
TypeError: comlink is not a function
    at file:///Users/ivan/Dev/vite-workers/vite.config.js?t=1653404879713:4:13
    at ModuleJob.run (node:internal/modules/esm/module_job:198:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:385:24)
    at async importModuleDynamicallyWrapper (node:internal/vm/module:437:15)
    at async loadConfigFromFile (/Users/ivan/Dev/vite-workers/node_modules/vite/dist/node/chunks/dep-59dc6e00.js:61843:31)
    at async resolveConfig (/Users/ivan/Dev/vite-workers/node_modules/vite/dist/node/chunks/dep-59dc6e00.js:61376:28)
    at async createServer (/Users/ivan/Dev/vite-workers/node_modules/vite/dist/node/chunks/dep-59dc6e00.js:59783:20)
    at async CAC.<anonymous> (/Users/ivan/Dev/vite-workers/node_modules/vite/dist/node/cli.js:688:24)
    ```

@mathe42 mathe42 reopened this May 24, 2022
@mathe42
Copy link
Owner

mathe42 commented May 24, 2022

I could reproduce that but have no idea why. I will investigate please use import {comlink} from 'vite-plugin-comlink' for now...

@mathe42
Copy link
Owner

mathe42 commented May 24, 2022

I have no idea why it works with my project and not yours. I published a new version 3.0.3 that didn't fix it. I will keep looking into it but not today.

@mathe42 mathe42 changed the title Possible issue in README default export doesn't work. May 24, 2022
@mathe42 mathe42 changed the title default export doesn't work. default export doesn't work. "TypeError: xxx is not a function" May 24, 2022
@mathe42
Copy link
Owner

mathe42 commented May 25, 2022

I have NO idea why it worked for me... I will change the readme for now until I find a solution...

@colecrouter
Copy link
Author

Normally, you just have to change

export function

To

export default function

Right? I can't explain the local reproduction issue, though.

@ivancuric
Copy link

ivancuric commented May 25, 2022

It's the "type": "module" in the project's package.json which breaks it. This instructs the project to use esm module resolution with node 16+

Seems that something is broken with the .mjs module, but this looks fine.

var src_default = comlink;
export {
  comlink,
  src_default as default
};

@mathe42
Copy link
Owner

mathe42 commented May 25, 2022

It uses the normal .js file in both cases. but I think you are right that this is the problem I'm not sure why node uses the default import the same as import * as comlink from 'vite-plugin-comlink'

@ivancuric
Copy link

ivancuric commented May 25, 2022

It does, but it should be using the .mjs I think.

I can't get it to load the mjs file. I've tried adding "type": "module" to your package.json, but then everything falls apart.

@mathe42
Copy link
Owner

mathe42 commented May 25, 2022

note that this might be a combination with vite problem as at vite startup the config file is compiled to JS (and I think the complete config is bundled to a single file internaly.) The output of that is interpreted as a commonjs (or with type=module a esm)-File.

@mathe42
Copy link
Owner

mathe42 commented May 25, 2022

What is the reason you want to set type: module in a vite project. You could just change all .js extensions to .mjs where you have some code for node... the type field doesn't matter for vite-plugins and files vite processes.

@ivancuric
Copy link

ivancuric commented May 25, 2022

I have it as it's kinda the new standard, not much else. I also had some errors with https://github.com/liuweiGL/vite-plugin-mkcert which I fixed by calling mkcert.default() instead of mkcert()

Could this be related to vitejs/vite#6623?

I also noticed that when compiling in lib mode in Vite, they recommend using the following package.json:

{
  "name": "my-lib",
  "files": ["dist"],
  "main": "./dist/my-lib.umd.js",
  "module": "./dist/my-lib.es.js",
  "exports": {
    ".": {
      "import": "./dist/my-lib.es.js",
      "require": "./dist/my-lib.umd.js"
    }
  }
}

Compared to yours it's missing the exports field.

@mathe42
Copy link
Owner

mathe42 commented May 25, 2022

Might be. exports field doesn't change anything. Reproduction is still broken. I think the best solution is to avoid the default import. vite 3 will have some changes with esm files etc. so I would not touch it for now and wait for the 3.0 release.

@mathe42
Copy link
Owner

mathe42 commented Jul 18, 2022

I know that vite 3 is out but I'm currently quite busy (I'm in my last year of University) so at least in the next 4-5 Weeks don't expect a fix. I didn't even had a look at vite 3 yet. (so also vite-plugin-comlink version 4 is pushed back). On npmjs I can see that a lot of persons only use an old version:

image

So there is no harm in waiting for v4 as it will have a number of breaking changes.

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

3 participants