microsoft / microsoft/TypeScript-Sublime-Plugin

"Cannot find module" when requiring node_module with package.json types from .js but works fine in .ts

Open
#768 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
1.7k
Forks
236
Avg merge
21d 13h
Merged PRs (30d)
1

Description

I have some npm dependencies like socket.io or cheerio that provide type info for TS via the package.json types field.

This works just fine in a TS file such as server.ts:

import socketIO = require('socket.io')
socketIO.blah // correctly warns about property not existing
socketIO.Socket // correctly knows this exists

In javascript files however I get en error on the require line, ex in server.js:

// @ts-check
const socketIO = require('socket.io') // "Cannot find module 'socket.io' or its corresponding type declarations"

I did try and add a reference path comment/hint but that didn't work in server.js:

// @ts-check
/// <reference path='./node_modules/socket.io/dist/index.d.ts' />
const socketIO = require('socket.io') // "Cannot find module 'socket.io' or its corresponding type declarations"

However, type info comes through just fine for anything I've had to use DefinitelyTyped for. So for example if I have express as a dependency and @types/express as a devDependency then this works in server.js:

// @ts-check
const express = require('express')
express.blah // correctly warns about property not existing
express.static // correctly knows this exists

Seems like something is missing here when it comes to reading the types field off of package.json for dependencies. Is this just broken/missing functionality for javascript files or am I just missing some kind configuration or something?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue in server.js with @ts-check and a socket.io dependency whose package.json has a types field, then compare the result with server.ts and the express/@types/express example. Start by tracing how the plugin resolves the require and package.json types entry; done means JavaScript imports resolve the bundled declarations and retain the reported property checking.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, node.js, typescript
Domain
developer-experience, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.