I’m working on a project and I’m stuck. My project folder looks like this:
my-project/
|-- test-stuff/
|-- package.json
I’m in the my-project
folder and I want to use eslint from the test-stuff
folder. I know I can install packages there using npm install --prefix test-stuff/
, but I haven’t found a similar option for npx. When I run npx eslint
, it downloads the package from the internet instead of using the version in test-stuff
. Is there a way to direct npx to use the package in that directory without changing folders?
I’ve faced a similar issue before, and I found a workaround that might help you out. Instead of using npx directly, you can leverage npm’s --prefix
option in combination with the npm exec
command. Here’s how you can do it:
npm --prefix test-stuff/ exec eslint
This command tells npm to use the packages from the test-stuff directory and then executes eslint from there. It’s not as concise as using npx, but it achieves the same result without downloading packages unnecessarily.
Alternatively, you could create a simple shell script or npm script in your root package.json that changes directory, runs the command, and then changes back. It’s a bit more setup, but it can make your workflow smoother in the long run.
Remember, these solutions assume you’re okay with running npm commands instead of npx directly. If you absolutely need to use npx for some reason, you might have to change directories or look into more complex solutions involving NODE_PATH environment variables.
You might find it helpful to use the --prefix
option with npx
directly. Try running:
npx --prefix test-stuff/ eslint
This tells npx to look for packages in the test-stuff directory. It’s a straightforward solution that doesn’t require changing your directory structure or creating additional scripts.
If you’re frequently running commands like this, consider setting up a shell alias or function to streamline the process. This approach maintains your current project structure while giving you the flexibility to use packages from specific directories as needed.
hey there! i’ve run into this before. one trick is to use the full path to npx in the test-stuff folder. like this:
./test-stuff/node_modules/.bin/npx eslint
it’s a bit clunky but it works without changing dirs. hope that helps!