question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot import certain npm packages when using TypeScript

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
yyx990803commented, Aug 3, 2018

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.

1reaction
ffxsamcommented, Aug 3, 2018

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';
Read more comments on GitHub >

github_iconTop Results From Across the Web

npm module can't import - Stack Overflow
I'm the maintainer of pickr,. since version 1.4.5 there should be official type-declarations if you're using it within typescript.
Read more >
Common TypeScript module problems and how to solve them
Enabling the compiler module resolution tracing in TypeScript can provide insight in diagnosing and solving problems.
Read more >
Cannot use import statement outside a module [React ...
When building a web application, you may encounter the SyntaxError: Cannot use import statement outside a module error.
Read more >
How To Use Modules in TypeScript | DigitalOcean
Modules are a way to organize your code into smaller, more manageable pieces, allowing programs to import code from different parts of the ......
Read more >
module-not-found - Next.js
When importing a module from npm this module has to be installed locally. ... is a Node.js specific library that can't run in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found