npx but use version in package json
Asked Answered
S

1

6

I have a build script in javascript project that uses parcel to iterate through folders and finding index files and then building those projects. When I find an index file I run this command:

npx parcel build ${indexFilePath} --out-dir ${outDir} --target node

But, if the person running this build script has not installed packages with npm i yet, the npx command will install the latest version of parcel-bundler, where --out-dir is now deprecated and breaking the entire script.

Here is what's going on, distilled:

$ rm -rf node_modules
$ npm i
$ npx parcel --version
1.12.4
$ rm -rf node_modules
$ npx parcel --version
2.0.0-beta.2

Is there any option for npx to use the version in the package.json file that is already in the project and install it to node_modules? Or do I just ingrate the install step into my build? It just seems overkill, since all I need to build is parcel.

Am I abusing npx by using it like this?

Sard answered 29/4, 2021 at 14:43 Comment(0)
S
4

Multiple issues exist in npx regarding this problem : #199 & #15

The only option I found so far was to parse & extract version from my package.json using node repl (which should be available in your case)

Something like this :

npx parcel@`node -p -e "require('./package.json').dependencies['parcel']"` --version
Salver answered 14/4, 2022 at 13:38 Comment(1)
Thanks for the heads up, I'll follow those issues. I ended up extracting version from package.json with jq, but I prefer this solution.Sard

© 2022 - 2024 — McMap. All rights reserved.