microsoft / TypeScript-Sublime-Plugin

IO wrapper around TypeScript language services, allowing for easy consumption by editor plugins
Apache License 2.0
1.72k stars 235 forks source link

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

Open ZeikJT opened 3 years ago

ZeikJT commented 3 years ago

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?