Skip to content

module: allow running .ts in node_modules if private#55385

Closed
marco-ippolito wants to merge 2 commits into
nodejs:mainfrom
marco-ippolito:module/enable-ts-node_modules
Closed

module: allow running .ts in node_modules if private#55385
marco-ippolito wants to merge 2 commits into
nodejs:mainfrom
marco-ippolito:module/enable-ts-node_modules

Conversation

@marco-ippolito

@marco-ippolito marco-ippolito commented Oct 14, 2024

Copy link
Copy Markdown
Member

Refs: nodejs/typescript#14

This PR checks when attempting to run .ts files under node_modules if the package.json contains the property private: true.

This should make possible to use strip-types in monorepos, without allowing package maintainer to publish their packages on npm without compiling first.

Also not very performant since is checking/parsing the package.json every time a file under node_modules is encountered, it requires some sort of caching

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/loaders
  • @nodejs/typescript

@nodejs-github-bot nodejs-github-bot added lib / src Issues and PRs related to general changes in the lib or src directory. needs-ci PRs that need a full CI run. labels Oct 14, 2024
@marco-ippolito marco-ippolito added the strip-types Issues or PRs related to strip-types support label Oct 14, 2024
@mcollina

Copy link
Copy Markdown
Member

This solution works for me.

Comment thread doc/api/errors.md

Type stripping is not supported for files descendent of a `node_modules` directory.
Type stripping is not supported for files descendent of a `node_modules` directory,
where package.json is missing or does not contain the property `"private": true`.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
where package.json is missing or does not contain the property `"private": true`.
where package.json is missing or does not contain the property `"private": true`.

Is there any chance it can be missing?

function isPrivateNodeModule(filename) {
const packageJsonReader = require('internal/modules/package_json_reader');
const resolved = StringPrototypeStartsWith(filename, 'file://') ? filename : pathToFileURL(filename).href;
const packageConfig = packageJsonReader.getPackageScopeConfig(resolved);

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we cache already analized paths?
Like everything under node_modules/foo once we have already read read node_modules/foo/package.json

@jakebailey

Copy link
Copy Markdown
Member

I said this on nodejs/typescript#14 (comment), but I don't understand the use case here; if you have a package like this, you can't have published it because it's marked private. How did you get the package, then? For monorepos, I would really expect that you'd get it via a symlink, in which case the realpath of the files will not prevent the transform from happening.

@marco-ippolito

Copy link
Copy Markdown
Member Author

I said this on nodejs/typescript#14 (comment), but I don't understand the use case here; if you have a package like this, you can't have published it because it's marked private. How did you get the package, then? For monorepos, I would really expect that you'd get it via a symlink, in which case the realpath of the files will not prevent the transform from happening.

Probably easier to use since it doesnt require additional tooling.
We could also document that possibility

@jakebailey

Copy link
Copy Markdown
Member

That doesn't really answer my question... I understand that using this flag prevents needing another tool like tsx. But how would someone end up in the situation where their node_modules directory contains a private package, but that package is not symlinked?

@marco-ippolito

Copy link
Copy Markdown
Member Author

I thought that private: true would still allow publishing privately on the registry, which is not the case 😐
In that case yes it doesnt make sense.

@bmeck

bmeck commented Oct 15, 2024

Copy link
Copy Markdown
Member

@marco-ippolito were you thinking about bundledDependencies?

@damianobarbati

Copy link
Copy Markdown

@marco-ippolito is this possible now? I am using pnpm deploy in my monorepo, and I marked all my package.json of the packages as private:true but I still get

Error [ERR_UNSUPPORTED_NODE_MODULES_TYPE_STRIPPING]: Stripping types is currently unsupported for files under node_modules, for "file:///node_modules/.pnpm/framework@file++++mnt+packages+framework/node_modules/framework/src/AppError.ts"

@marco-ippolito

Copy link
Copy Markdown
Member Author

@marco-ippolito is this possible now? I am using pnpm deploy in my monorepo, and I marked all my package.json of the packages as private:true but I still get

Error [ERR_UNSUPPORTED_NODE_MODULES_TYPE_STRIPPING]: Stripping types is currently unsupported for files under node_modules, for "file:///node_modules/.pnpm/framework@file++++mnt+packages+framework/node_modules/framework/src/AppError.ts"

Nope there is no consensus on landing this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

lib / src Issues and PRs related to general changes in the lib or src directory. needs-ci PRs that need a full CI run. strip-types Issues or PRs related to strip-types support

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants