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

Fixed: For making TS notebook in Windows #444

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

aakash-a-dev
Copy link
Contributor

Documentation for Fixing spawn npm ENOENT Error with pnpm dlx

Issue Overview

The error spawn npm ENOENT occurred when trying to spawn a tsserver instance using npx in a Node.js script. This error typically happens when npm is either missing or not found in the system's PATH, which is required for npx to function correctly. Although npx is supposed to work independently, it relies on npm being available in the environment. Since the project is using pnpm as the package manager, the solution is to use pnpm dlx instead of npx.


Before Resolving the Issue the Error

Error

After Resolving the Issue the Error

Output-2

image

How I Resolved the Issue

  1. Replace npx with pnpm dlx:

    The command that spawns the tsserver instance was originally using npx:

    const child = spawn('npx', ['tsserver'], {
      cwd: options.cwd,
      shell: true
    });

    This was changed to use pnpm dlx to make it compatible with pnpm:

    const child = spawn('pnpm', ['dlx', 'tsserver'], {
      cwd: options.cwd,
      shell: true
    });

    The pnpm dlx command is similar to npx but uses pnpm to run the specified package without requiring global installation. It ensures that pnpm is handling the execution, avoiding the spawn npm ENOENT error.


Changes Made to package.json

No changes were directly made to package.json to resolve the error. However, to ensure that pnpm dlx can be used smoothly, make sure the following:

  1. Added rimraf Dependency:

    "devDependencies": {
      "rimraf": "^6.0.1",
      ...
    }

Conclusion

By switching from npx to pnpm dlx, the spawn npm ENOENT error was resolved. This solution is optimal for projects using pnpm as the package manager, ensuring compatibility and proper execution of the tsserver without relying on npm's presence in the environment.

PS: This solution doesn't turn off the server, which was mentioned in the issue, and it also gives the output in my Windows system.

Fix: #297

@@ -44,8 +44,9 @@ export class TsServers {
// created, the dependencies are not installed and thus this will
// shut down immediately. Make sure that we handle this case after
// package.json has finished installing its deps.
const child = spawn('npx', ['tsserver'], {
const child = spawn('pnpm', ['dlx' ,'tsserver'], {
Copy link
Contributor

Choose a reason for hiding this comment

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

We don't want to do this, because pnpm is a dependency for dev, but not for users. This would require our users to have pnpm installed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ooo okay, I didn't think that way. I will look into this further. Thank you!

@aakash-a-dev
Copy link
Contributor Author

This approach ensures portability and compatibility by directly resolving the tsserver binary from the project's node_modules, avoiding reliance on global tools like pnpm or npx.

Key Features of the Solution

  1. No Global Dependencies:

    • Avoids relying on tools like pnpm or npx, which require global installation. Only depends on the local node_modules directory.
  2. Cross-Platform Compatibility:

    • Automatically resolves to tsserver.cmd on Windows and tsserver on Linux/macOS, ensuring it works across all operating systems.
  3. Version Consistency:

    • Always uses the typescript version installed in the project, ensuring consistency between development environments and deployments.
  4. Shell-Free Execution:

    • Directly runs the tsserver binary without relying on shell commands, improving security and reducing potential issues.

Output

image

image

I hope this solution will be helpful for dev and end user both!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Creating ts srcbook on windows
2 participants