vue-multiselect icon indicating copy to clipboard operation
vue-multiselect copied to clipboard

Cannot use import statement outside a module

Open jbrooksuk opened this issue 2 years ago • 13 comments

When using vue-multiselect="^3.0.0-alpha.2" I get this error when running with SSR:

/Users/james/Code/laravel/forge/node_modules/vue-multiselect/dist/vue-multiselect.esm.js:1
import { openBlock, createBlock, withKeys, withModifiers, renderSlot, createVNode, withDirectives, Fragment, renderList, toDisplayString, vShow, createCommentVNode, Transition, withCtx, createTextVNode } from 'vue';
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at Object.compileFunction (node:vm:352:18)
    at wrapSafe (node:internal/modules/cjs/loader:1026:15)
    at Module._compile (node:internal/modules/cjs/loader:1061:27)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1151:10)
    at Module.load (node:internal/modules/cjs/loader:975:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Module.require (node:internal/modules/cjs/loader:999:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.5484 (/Users/james/Code/laravel/forge/public/js/ssr.js:1:1540400)
    at o (/Users/james/Code/laravel/forge/public/js/ssr.js:1:1540654)

jbrooksuk avatar Mar 18 '22 10:03 jbrooksuk

Has anyone managed to resolve this at all? I'm building with Laravel Mix and I've tried transpiling during compilation with no luck.

jbrooksuk avatar May 04 '22 11:05 jbrooksuk

I got the same error when running tests with Jest and this transformIgnorePatterns helped:

"transformIgnorePatterns": [
	"/node_modules/(?!(.*vue-multiselect))"
],

raimund-schluessler avatar May 05 '22 20:05 raimund-schluessler

Hi @jbrooksuk. Thanks for opening up an issue around this. I'm unfamiliar with SSR, so are you able to create a reproducible repo so I can figure out how best we can fix this?

mattelen avatar Aug 19 '22 14:08 mattelen

@jbrooksuk did you manage to find a fix?

davimug avatar Feb 08 '23 00:02 davimug

I came up with the same problem using vue3 and Jest v28. After digging I found out Jest is actually trying to load vue-multiselect from this file vue-multiselect/dist/vue-multiselect.esm.js instead of this file vue-multiselect/dist/vue3-multiselect.common.js.

So one solution is the following, put this in your jest.config.js file :

moduleNameMapper: {
    '^vue-multiselect$': 'vue-multiselect/dist/vue3-multiselect.common.js'
},

N.B : Using a transformIgnorePatterns wasn't a solution in my case, and it seems logical since it's not looking at the good file...

Hope this can help some coming this way.

paul-jolimoi avatar Mar 28 '23 08:03 paul-jolimoi

Hi! I have the same problem using 3.0.0-beta.2 and Vite 4.x with `import Multiselect from 'vue-multiselect'.

For me it works by adding "type": "module", to vue-multiselect package.json.

Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.

phlegx avatar May 26 '23 13:05 phlegx

@paul-jolimoi in my case

'^vue-multiselect$': 'vue-multiselect/dist/vue-multiselect.common.js',

with vue-multiselect instead of vue3-multiselect.

gustawdaniel avatar Jun 01 '23 00:06 gustawdaniel

If in an App where use vue-multiselect ^3.0.0-beta.2 witch SSR You use Vite, then you need to explicitly specify in vite.config.js in the build section the target option in 'esnext', which will add when building support for dynamic imports.

Example config for Laravel + InertiaJS + Vite application:

import { defineConfig } from 'vite'
import laravel from 'laravel-vite-plugin'
import vue from '@vitejs/plugin-vue'

export default defineConfig({
  build: {
    target: 'esnext', // <---
    sourcemap: true
  },
  plugins: [
    laravel({
      input: 'resources/js/app.js',
      ssr: 'resources/js/ssr.js',
      refresh: true
    }),
    vue({
      template: {
        compilerOptions: { // ts config
          isCustomElement: (tag) => tag.startsWith('swiper-') // <--- use swipper slider
        },
        transformAssetUrls: {
          base: null,
          includeAbsolute: false
        }
      }
    })
  ]
})

Problem discussion on Stackoverflow

Documentation: https://vitejs.dev/config/build-options.html#build-target

Seeman13 avatar Jun 07 '23 12:06 Seeman13

@Seeman13 thx! But what if you dont want to use esnext and work with legacy plugin?

phlegx avatar Jun 07 '23 14:06 phlegx

@Seeman13 thx! But what if you dont want to use esnext and work with legacy plugin?

Well then, if you work with a deprecated plugin, then use Vue 2 or even better Vue 1 - and work with everything outdated! If you use the new, and then need work with the new!

Seeman13 avatar Jun 07 '23 15:06 Seeman13

Hi! I have the same problem using 3.0.0-beta.2 and Vite 4.x with `import Multiselect from 'vue-multiselect'.

For me it works by adding "type": "module", to vue-multiselect package.json.

Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.

Thanks a lot, man! Managed to sort out that same issue on my end using this workaround.

jet4419 avatar Aug 08 '23 02:08 jet4419