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

Can't import the named export from non EcmaScript module (only default export is available) #675

Closed
MycroftHolmes1989 opened this issue Sep 14, 2021 · 17 comments · Fixed by #694
Labels
bug Something isn't working has workaround The issue contains a temporary solution to get around the problem

Comments

@MycroftHolmes1989
Copy link

MycroftHolmes1989 commented Sep 14, 2021

Steps to reproduce the behavior

  1. Use Vue-cli to create a Vue 3 app with Typescript.
  2. Run yarn add pinia@next.
  3. Edit the main.ts:
import { createApp } from "vue";
import App from "./App.vue";
import { createPinia } from "pinia";

const app = createApp(App);
app.use(createPinia());
app.mount("#app");
  1. Run yarn serve.

Expected behavior

The compilation goes without an error

Actual behavior

Throws error:

ERROR  Failed to compile with 18 errors                                                                                                           

 error  in ./node_modules/pinia/dist/pinia.mjs

Can't import the named export 'computed' from non EcmaScript module (only default export is available)

 error  in ./node_modules/pinia/dist/pinia.mjs

Can't import the named export 'del' from non EcmaScript module (only default export is available)

 error  in ./node_modules/pinia/dist/pinia.mjs

Can't import the named export 'effectScope' from non EcmaScript module (only default export is available)

 error  in ./node_modules/pinia/dist/pinia.mjs

Can't import the named export 'getCurrentInstance' from non EcmaScript module (only default export is available)

 error  in ./node_modules/pinia/dist/pinia.mjs

Can't import the named export 'inject' from non EcmaScript module (only default export is available)

 error  in ./node_modules/pinia/dist/pinia.mjs

Can't import the named export 'isReactive' from non EcmaScript module (only default export is available)

 error  in ./node_modules/pinia/dist/pinia.mjs

Can't import the named export 'isRef' from non EcmaScript module (only default export is available)

 error  in ./node_modules/pinia/dist/pinia.mjs

Can't import the named export 'isVue2' from non EcmaScript module (only default export is available)

 error  in ./node_modules/pinia/dist/pinia.mjs

Can't import the named export 'markRaw' from non EcmaScript module (only default export is available)

 error  in ./node_modules/pinia/dist/pinia.mjs

Can't import the named export 'onUnmounted' from non EcmaScript module (only default export is available)

 error  in ./node_modules/pinia/dist/pinia.mjs

Can't import the named export 'reactive' from non EcmaScript module (only default export is available)

 error  in ./node_modules/pinia/dist/pinia.mjs

Can't import the named export 'ref' from non EcmaScript module (only default export is available)

 error  in ./node_modules/pinia/dist/pinia.mjs

Can't import the named export 'set' from non EcmaScript module (only default export is available)

 error  in ./node_modules/pinia/dist/pinia.mjs

Can't import the named export 'toRaw' from non EcmaScript module (only default export is available)

 error  in ./node_modules/pinia/dist/pinia.mjs

Can't import the named export 'toRef' from non EcmaScript module (only default export is available)

 error  in ./node_modules/pinia/dist/pinia.mjs

Can't import the named export 'toRefs' from non EcmaScript module (only default export is available)

 error  in ./node_modules/pinia/dist/pinia.mjs

Can't import the named export 'unref' from non EcmaScript module (only default export is available)

 error  in ./node_modules/pinia/dist/pinia.mjs

Can't import the named export 'watch' from non EcmaScript module (only default export is available)

Additional Information:

"dependencies": {
   "core-js": "^3.6.5",
   "pinia": "^2.0.0-rc.9",
   "primeflex": "^3.0.1",
   "primeicons": "^4.1.0",
   "primevue": "^3.7.1",
   "vue": "^3.0.0",
   "vue-router": "^4.0.0-0"
 },
 "devDependencies": {
   "@typescript-eslint/eslint-plugin": "^4.18.0",
   "@typescript-eslint/parser": "^4.18.0",
   "@vue/cli-plugin-babel": "~4.5.0",
   "@vue/cli-plugin-eslint": "~4.5.0",
   "@vue/cli-plugin-router": "~4.5.0",
   "@vue/cli-plugin-typescript": "~4.5.0",
   "@vue/cli-service": "~4.5.0",
   "@vue/compiler-sfc": "^3.0.0",
   "@vue/eslint-config-prettier": "^6.0.0",
   "@vue/eslint-config-typescript": "^7.0.0",
   "eslint": "^6.7.2",
   "eslint-plugin-prettier": "^3.3.1",
   "eslint-plugin-vue": "^7.0.0",
   "lint-staged": "^9.5.0",
   "node-sass": "^4.12.0",
   "prettier": "^2.2.1",
   "sass-loader": "^8.0.2",
   "typescript": "~4.1.5"
 },
@Liwoj
Copy link

Liwoj commented Sep 14, 2021

Same problem here. Using "typescript": "~4.3.5".
Downgrading back to 2.0.0-rc.8 fixes the issue...

@Liwoj
Copy link

Liwoj commented Sep 14, 2021

Seems related and also the workaround mentioned in the discussion works.

But it's strange as all the issues I found (here, here) describe this problem as "unable to do named import from CJS into ESM module". But VueDemi provides ESM build and after checking Webpack 4 docs (as Vue CLI is still on WP4), it has also correct settings in package.json ("module" field pointing to .mjs file).

So I still don't understand why it is treated as "non EcmaScript module"

@posva posva added the need repro This issue couldn't be reproduced label Sep 14, 2021
@posva
Copy link
Member

posva commented Sep 14, 2021

Can someone provide a boiled down repro with only Vue and pinia (no router, or other unnecessary steps)

@jhony-v
Copy link

jhony-v commented Sep 14, 2021

I had the same problem but I solved editing my vue.config.js file with the following webpack rule:

// vue.config.js
module.exports = {
  configureWebpack: {
    module: {
      rules: [
        {
          test: /\.mjs$/,
          include: /node_modules/,
          type: "javascript/auto"
        }
      ] 
    }
  }
}

With this configuration it worked.

@predragnikolic
Copy link

Can someone provide a boiled down repro with only Vue and pinia (no router, or other unnecessary steps)

@posva I created a vue-cli project with a repro. Hope it helps.

https://github.com/predragnikolic/pinia-bug-repro

And when I try to import pinia:
https://github.com/predragnikolic/pinia-bug-repro/commit/d9810039d701d30d58f11688f3081fe14545db4a

I get errors when running yarn serve:

yarn serve
yarn run v1.22.11
warning ../package.json: No license field
$ vue-cli-service serve
 INFO  Starting development server...
98% after emitting CopyPlugin

 ERROR  Failed to compile with 18 errors                              5:09:52 PM

 error  in ./node_modules/pinia/dist/pinia.mjs

Can't import the named export 'computed' from non EcmaScript module (only default export is available)

 error  in ./node_modules/pinia/dist/pinia.mjs

Can't import the named export 'del' from non EcmaScript module (only default export is available)

 error  in ./node_modules/pinia/dist/pinia.mjs

Can't import the named export 'effectScope' from non EcmaScript module (only default export is available)

 error  in ./node_modules/pinia/dist/pinia.mjs

Can't import the named export 'getCurrentInstance' from non EcmaScript module (only default export is available)

 error  in ./node_modules/pinia/dist/pinia.mjs

Can't import the named export 'inject' from non EcmaScript module (only default export is available)

 error  in ./node_modules/pinia/dist/pinia.mjs

Can't import the named export 'isReactive' from non EcmaScript module (only default export is available)

 error  in ./node_modules/pinia/dist/pinia.mjs

Can't import the named export 'isRef' from non EcmaScript module (only default export is available)

 error  in ./node_modules/pinia/dist/pinia.mjs

Can't import the named export 'isVue2' from non EcmaScript module (only default export is available)

 error  in ./node_modules/pinia/dist/pinia.mjs

Can't import the named export 'markRaw' from non EcmaScript module (only default export is available)

 error  in ./node_modules/pinia/dist/pinia.mjs

Can't import the named export 'onUnmounted' from non EcmaScript module (only default export is available)

 error  in ./node_modules/pinia/dist/pinia.mjs

Can't import the named export 'reactive' from non EcmaScript module (only default export is available)

 error  in ./node_modules/pinia/dist/pinia.mjs

Can't import the named export 'ref' from non EcmaScript module (only default export is available)

 error  in ./node_modules/pinia/dist/pinia.mjs

Can't import the named export 'set' from non EcmaScript module (only default export is available)

 error  in ./node_modules/pinia/dist/pinia.mjs

Can't import the named export 'toRaw' from non EcmaScript module (only default export is available)

 error  in ./node_modules/pinia/dist/pinia.mjs

Can't import the named export 'toRef' from non EcmaScript module (only default export is available)

 error  in ./node_modules/pinia/dist/pinia.mjs

Can't import the named export 'toRefs' from non EcmaScript module (only default export is available)

 error  in ./node_modules/pinia/dist/pinia.mjs

Can't import the named export 'unref' from non EcmaScript module (only default export is available)

 error  in ./node_modules/pinia/dist/pinia.mjs

Can't import the named export 'watch' from non EcmaScript module (only default export is available)

No issues found.

@posva
Copy link
Member

posva commented Sep 27, 2021

@predragnikolic that's a lot of projects 😓

@MycroftHolmes1989 can you create a boiled down reproduction please?

@timpulver
Copy link

timpulver commented Sep 28, 2021

Here is the chained Webpack config if someone else needs it:

vue.config.js:

/**
 * @type {import('@vue/cli-service').ProjectOptions}
 */
module.exports = {
  chainWebpack: (config) => {
    config.module
      .rule("mjs")
      .test(/\.mjs$/)
      .type("javascript/auto")
      .include.add(/node_modules/)
      .end();
  },
};

@posva
Copy link
Member

posva commented Sep 29, 2021

I reproduced the issue on a simple project. This should be fixable by changing the extensions of the generated files. I still need to test it out a bit more.

@posva posva added bug Something isn't working has workaround The issue contains a temporary solution to get around the problem and removed need repro This issue couldn't be reproduced labels Sep 29, 2021
@tobiasdiez
Copy link

I get this error now when trying to upgrade from @pinia/nuxt v 0.0.4 to version 0.0.6, see JabRef/JabRefOnline#551. The reason might be 438b16d ?

@4Kazelot
Copy link

I had the same problem but I solved editing my vue.config.js file with the following webpack rule:

// vue.config.js
module.exports = {
  configureWebpack: {
    module: {
      rules: [
        {
          test: /\.mjs$/,
          include: /node_modules/,
          type: "javascript/auto"
        }
      ] 
    }
  }
}

With this configuration it worked.

For NuxtJS:

export default {
  build: {
    extend(config) {
      config.module.rules.push({
        test: /\.mjs$/,
        include: /node_modules/,
        type: 'javascript/auto',
      })
    },
  },
}

@Kooklen
Copy link

Kooklen commented Jan 24, 2022

I had the same problem but I solved editing my vue.config.js file with the following webpack rule:

// vue.config.js
module.exports = {
  configureWebpack: {
    module: {
      rules: [
        {
          test: /\.mjs$/,
          include: /node_modules/,
          type: "javascript/auto"
        }
      ] 
    }
  }
}

With this configuration it worked.

It works

@qrtp
Copy link

qrtp commented Feb 18, 2022

I had the same problem but I solved editing my vue.config.js file with the following webpack rule:

// vue.config.js
module.exports = {
  configureWebpack: {
    module: {
      rules: [
        {
          test: /\.mjs$/,
          include: /node_modules/,
          type: "javascript/auto"
        }
      ] 
    }
  }
}

With this configuration it worked.

For NuxtJS:

export default {
  build: {
    extend(config) {
      config.module.rules.push({
        test: /\.mjs$/,
        include: /node_modules/,
        type: 'javascript/auto',
      })
    },
  },
}

@4Kazelot For the NuxtJS what file does this config go in?

@AzimovAzimDev
Copy link

[[[> > > I had the same problem but I solved editing my vue.config.js file with the following webpack rule:

// vue.config.js
module.exports = {
  configureWebpack: {
    module: {
      rules: [
        {
          test: /\.mjs$/,
          include: /node_modules/,
          type: "javascript/auto"
        }
      ] 
    }
  }
}

With this configuration it worked.

For NuxtJS:

export default {
  build: {
    extend(config) {
      config.module.rules.push({
        test: /\.mjs$/,
        include: /node_modules/,
        type: 'javascript/auto',
      })
    },
  },
}

@4Kazelot For the NuxtJS what file does this config go in?

https://nuxtjs.org/docs/features/configuration/#extend-webpack-config](https://nuxtjs.org/docs/features/configuration/#extend-webpack-config)](https://nuxtjs.org/docs/features/configuration/#extend-webpack-config)](https://nuxtjs.org/docs/features/configuration/#extend-webpack-config)

@taidaid
Copy link

taidaid commented Jul 1, 2022

Hello, we are trying to use Pinia alongside Vuex in a Nuxt 2 project in order to smooth our eventual migration to Nuxt 3. However, even after adding

    extend(config) {
      config.module.rules.push({
        test: /\.mjs$/,
        include: /node_modules/,
        type: 'javascript/auto',
      })
    },

to our build config, we are still seeing the Can't import the named export 'isVue2' from non EcmaScript module (only default export is available). The error comes from our ./.nuxt/dist.plugin.209980bd.mjs file which seems to be the @pinia/nuxt plugin setup.

Here is some of our package.json

// dependencies
"nuxt": "2.15.8",
"vue": "2.6.14",
"@nuxtjs/composition-api": "^0.23.2" ,
"@pinia/nuxt": "^0.1.9",
"pinia": "^2.0.14

// devDependencies
"@nuxt/typescript-build": "2.1.0"

We tried some experiments such as deleting ths .cjs file from vue-demi, but it seems that no matter what we do, the file is not reading the .mjs file correctly.

@andreisucman
Copy link

If nothing worked you can try importing differently.

Instead of

import {a, b} from "c";

try

import * as q from "c";

and then

const {a, b} = q;

@Kameecoding

This comment was marked as outdated.

@yudhishthir-siterecon
Copy link

If you are using CRACO for react app, add the following webpack configuration to your craco.config.js:

  module.exports = {
    webpack: {
      configure: (webpackConfig, { env, paths }) => {
        // Add a rule to handle .mjs files
        webpackConfig.module.rules.push({
          test: /\.mjs$/,
          include: /node_modules/,
          type: 'javascript/auto',
        });
  
        return webpackConfig;
      },
    },
  };

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working has workaround The issue contains a temporary solution to get around the problem
Projects
None yet
Development

Successfully merging a pull request may close this issue.