Vue-cli: Cannot import certain npm packages when using TypeScript

Created on 3 Aug 2018  路  7Comments  路  Source: vuejs/vue-cli

Version

3.0.0-rc.10

Node and OS info

Node 8.10, yarn 1.9.2, macOS 10.13.6

Steps to reproduce

https://github.com/ffxsam/repro-import-bug

  1. Clone repo
  2. Run yarn && yarn serve
  3. See critical error.

What is expected?

I expect the app to run with no issues.

What is actually happening?

Error is given: Cannot invoke an expression whose type lacks a call signature. Type 'typeof moment' has no compatible call signatures.


Using import moment from 'moment' also does not work.

Most helpful comment

This seems to be a bug in TypeScript, because per its documentation, allowSyntheticDefaultImports should be true by default when esModuleInterop is true.

In this case, it only works properly when allowSyntheticDefaultImports is explicitly set to true.

Note: Vue CLI simply scaffolds a standard usage for upstream deps like webpack and typescript, it doesn't do any magic beyond the tsconfig.json in your project. In the future, please try to locate your issues in upstream tools first before opening an issue in Vue CLI.

All 7 comments

Some other imports require different syntax now. For instance, in a traditional JS project, this is fine:

import validator from 'validator';

In a TS project, it's not:

ERROR in /Users/samh/repro-import-bug/src/components/HelloWorld.vue
38:8 Module '"validator"' has no default export.

This fix works for validator, but does _not_ work for moment:

import * as validator from 'validator';

This seems to be a bug in TypeScript, because per its documentation, allowSyntheticDefaultImports should be true by default when esModuleInterop is true.

In this case, it only works properly when allowSyntheticDefaultImports is explicitly set to true.

Note: Vue CLI simply scaffolds a standard usage for upstream deps like webpack and typescript, it doesn't do any magic beyond the tsconfig.json in your project. In the future, please try to locate your issues in upstream tools first before opening an issue in Vue CLI.

Closed via 350f77b9

Ah, gotcha. Still, glad to see a simple tsconfig.json change in the Vue TS template fixes it!

Just came across this comment:

We only consider esModuleInterop to imply allowSyntheticDefaultimports when the module target is commonjs, amd, umd, or system.

So it's a bug in their documentation. 馃檨

Should be fixed by https://github.com/Microsoft/TypeScript-Handbook/commit/8e974c814d44381ed05a3b6203894e0f9e39d552, and we'll update the website documentation soon. Sorry about all this.

@DanielRosenwasser Ahh, yes. My module prop is set to esnext. Thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

NathanKleekamp picture NathanKleekamp  路  3Comments

eladcandroid picture eladcandroid  路  3Comments

BusyHe picture BusyHe  路  3Comments

PrimozRome picture PrimozRome  路  3Comments

jgribonvald picture jgribonvald  路  3Comments