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

What works in require results in module not found when using import #18431

Closed
RevealedFrom opened this issue Sep 13, 2017 · 5 comments
Closed
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@RevealedFrom
Copy link

TypeScript Version: 2.3

Visual Studio 2017 15.3.3 Basic Node.js Express Application project

I have the module emailjs (https://github.com/eleith/emailjs) installed globally. It is located in C:\Users\xxx\AppData\Roaming\npm\node_modules. This path is set in the project properties.

If I use var email = require("emailjs/email"), it works fine.

But if I use import * as email from "emailjs/email", it will fail with:

Build: Cannot find module 'emailjs/email'.

@RevealedFrom
Copy link
Author

Same build error with:

import * as config from "../config.json"

But following is fine:

var config = require("../config.json")

@mhegazy
Copy link
Contributor

mhegazy commented Sep 14, 2017

var config = require("../config.json") "works" because you have a function declaration for require somewhere in your project that looks like declare function require(a: string): any. The compiler was not doing any module resolution in this case.

For importing .json files in node, please see #7071.

See https://www.typescriptlang.org/docs/handbook/module-resolution.html for more information about how the compiler resolves modules.

@mhegazy mhegazy added the Working as Intended The behavior described is the intended behavior; this is not a bug label Sep 14, 2017
@RevealedFrom
Copy link
Author

Does it mean that, for the two examples listed, the modules are actually resolvable by the run time, but it is just the compiler blocking the build?

@kitsonk
Copy link
Contributor

kitsonk commented Sep 15, 2017

It means that the compiler does not have sufficient information to know they are resolvable at runtime. TypeScript cannot predict the future, it can only interpret the information you provide it.

@mhegazy
Copy link
Contributor

mhegazy commented Oct 2, 2017

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@mhegazy mhegazy closed this as completed Oct 2, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

3 participants