denoland / deno

A modern runtime for JavaScript and TypeScript.
https://deno.com
MIT License
93.74k stars 5.21k forks source link

Allow TypeScript files to be imported from npm packages #24093

Closed kruncher closed 2 months ago

kruncher commented 2 months ago

I am getting the following error when attempting to import a .ts file from an npm package (byonm):

TypeScript files are not supported in npm packages:

import { foo } from "some-node-module/abc.ts";
foo();

The source of the above error seems to be this: https://github.com/denoland/deno/blob/main/ext/node/resolution.rs#L454

There is a lot that I really like about deno but there are many scenarios where I want to be able to depend upon an entire package of files (scripts, stylesheets, templates, static assets, etc). There are many reasons where I find this to be useful; one such example is that of a reusable component library containing nunjucks templates and .scss files which can then be reused as a package across multiple services.

I would like to be able to reference a node module and then to import TypeScript files as-is and to be able to configure nunjucks to read templates from a directory structure from a package, and to be able to process .scss files where some are imported from a package.

In the node world npm packages quite easily allow for this and I think deno could potentially allow for this with its byonm feature. (As an aside I prefer to reference my own packages from GitHub repositories rather than publishing them to the npm registry).

dsherret commented 2 months ago

Took me a while to find, but duplicate of https://github.com/denoland/deno/issues/16790

In the node world npm packages quite easily allow for this and I think deno could potentially allow for this with its byonm feature. (As an aside I prefer to reference my own packages from GitHub repositories rather than publishing them to the npm registry).

Node doesn't support TS files and so the Node compatibility layer aligns with Node.

I would like to be able to reference a node module and then to import TypeScript files as-is and to be able to configure nunjucks to read templates from a directory structure from a package, and to be able to process .scss files where some are imported from a package.

I believe you should be able to do this, minus the importing the TypeScript file as is. You should be able to import any js file in the package though.

kruncher commented 2 months ago

@dsherret I can appreciate your thoughts on the linked issue regarding pollution of npm with TypeScript modules although I believe it is possible to import .ts modules from other packages given the right configuration.

Instead of using node_modules as a workaround I'd prefer to see some sort of equivalent in deno. Otherwise the irony is that people will end up building deno packages to npm just to be able to have packages when instead it would be cleaner to avoid that altogether by simply having the option to have deno packages.

eg. instead of node_modules being a first class citizen in deno, have deno packages (or even a generalised package mechanism) which deno is able to import typescript scripts from.

Please don't take this the wrong way, this isn't intended to be salty. I am expressing what I would personally like to be able to do with deno.