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

[Bug]: yarn PNP does not pass NODE_PATH to node #3833

Closed
1 task
alamothe opened this issue Dec 2, 2021 · 3 comments
Closed
1 task

[Bug]: yarn PNP does not pass NODE_PATH to node #3833

alamothe opened this issue Dec 2, 2021 · 3 comments
Labels
bug Something isn't working stale Issues that didn't get attention

Comments

@alamothe
Copy link

alamothe commented Dec 2, 2021

Self-service

  • I'd be willing to implement a fix

Describe the bug

We have a TypeScript project. To run this project in production, we first build it, then execute it with node.

For the build, we use plain tsc (no Babel) because it is a simple backend service intended to run on Node.js.

Until now, we enjoyed absolute imports through NODE_PATH. The idea is simple:

  • TypeScript compiler knows about absolute imports. However, tsc does not translate absolute imports to relative in generated js files, it leaves them as absolute.
  • Our source is in src, build goes to build.

So far, we ran the service in production with:

cross-env NODE_PATH=build node --max-old-space-size=2048 build/index.js

This worked great so far.

However, with Yarn PNP NODE_PATH is not passed to node and absolute imports no longer work. We simply cannot start the service in production anymore after migrating to PNP.

Someone suggested to take a look at link: which is described here: https://yarnpkg.com/features/protocols#why-is-the-link-protocol-recommended-over-aliases-for-path-mapping. However, that works only for plain JavaScript workspaces. Linking src makes no sense since our build goes to build, src only contains TypeScript and we have no issues with compiling.

To reproduce

  1. Create a TypeScript workspace and use absolute imports. TS has support for absolute imports through baseUrl setting.
  2. Build the code using tsc. So tsconfig.json looks like this:
{
	"compilerOptions": {
		"target": "es2019",
		"module": "commonjs",
		"lib": ["es2019"],

		"outDir": "build",
		"rootDir": "src",
		"baseUrl": "./src",

Which is very standard for TS.

  1. Try to run with:
cross-env NODE_PATH=build node --max-old-space-size=2048 build/index.js

Environment

System:
    OS: macOS 11.6.1
    CPU: (16) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
  Binaries:
    Node: 14.17.1 - /private/var/folders/nt/2q41tjws39n1pds7hmbyxwvm0000gn/T/xfs-74a6c0d3/node
    Yarn: 3.1.0 - /private/var/folders/nt/2q41tjws39n1pds7hmbyxwvm0000gn/T/xfs-74a6c0d3/yarn
    npm: 6.14.13 - /usr/local/opt/node@14/bin/npm

Additional context

I believe the fix is simple: yarn should pass existing NODE_PATH to node (it can append whatever it needs)

@alamothe alamothe added the bug Something isn't working label Dec 2, 2021
@yarnbot
Copy link
Collaborator

yarnbot commented Jan 1, 2022

Hi! 👋

This issue looks stale, and doesn't feature the reproducible label - which implies that you didn't provide a working reproduction using Sherlock. As a result, it'll be closed in a few days unless a maintainer explicitly vouches for it or you edit your first post to include a formal reproduction (you can use the playground for that).

Note that we require Sherlock reproductions for long-lived issues (rather than standalone git repositories or similar) because we're a small team. Sherlock gives us the ability to check which bugs are still affecting the master branch at any given point, and decreases the amount of code we need to run on our own machines (thus leading to faster bug resolutions). It helps us help you! 😃

If you absolutely cannot reproduce a bug on Sherlock (for example because it's a Windows-only issue), a maintainer will have to manually add the upholded label. Thanks for helping us triaging our repository! 🌟

@yarnbot yarnbot added the stale Issues that didn't get attention label Jan 1, 2022
@arcanis
Copy link
Member

arcanis commented Jan 1, 2022

NODE_PATH isn't compatible with PnP. You should instead define build via a link: dependency.

@merceyz
Copy link
Member

merceyz commented Jan 22, 2022

As I said on Discord, it's not that it isn't passed to Node, it's that PnP doesn't use it.

While the link: protocol doesn't work for you imports using conditions would which can be tracked in #3843

@merceyz merceyz closed this as completed Jan 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working stale Issues that didn't get attention
Projects
None yet
Development

No branches or pull requests

4 participants