electric-skeptic
electric-skeptic

Reputation: 21

Does NPX really need NPM to preinstall babel components to run it?

Why doesn't this work?
npx -p @babel/core -p @babel/cli -p @babel/preset-env babel --presets=@babel/preset-env src -d deploy

This does work, but I didn't think preinstalling was necessary with NPX:
npm i @babel/core @babel/cli @babel/preset-env && npx babel --presets=@babel/preset-env src -d deploy

By most descriptions it seems it should retrieve anything it can't find locally from the NPM registry, then either install it prior to running, or even just be able to run it without installation.

For my current project it's being included in a Heroku application build script, where the reported error is as follows:

remote: npx: installed 302 in 16.391s
remote: { Error: Cannot find module '@babel/preset-env' from '/tmp/build_d9c5cb59d8e4131a23bd0ce2437051f9'
remote: at Function.module.exports [as sync] (/tmp/npmcache.qz5Sl/_npx/595/lib/node_modules/@babel/core/node_modules/resolve/lib/sync.js:74:15)
remote: at resolveStandardizedName (/tmp/npmcache.qz5Sl/_npx/595/lib/node_modules/@babel/core/lib/config/files/plugins.js:101:31)
remote: at resolvePreset (/tmp/npmcache.qz5Sl/_npx/595/lib/node_modules/@babel/core/lib/config/files/plugins.js:58:10)
remote: at loadPreset (/tmp/npmcache.qz5Sl/_npx/595/lib/node_modules/@babel/core/lib/config/files/plugins.js:77:20)
remote: at createDescriptor (/tmp/npmcache.qz5Sl/_npx/595/lib/node_modules/@babel/core/lib/config/config-descriptors.js:154:9)
remote: at items.map (/tmp/npmcache.qz5Sl/_npx/595/lib/node_modules/@babel/core/lib/config/config-descriptors.js:109:50)
remote: at Array.map ()
remote: at createDescriptors (/tmp/npmcache.qz5Sl/_npx/595/lib/node_modules/@babel/core/lib/config/config-descriptors.js:109:29)
remote: at createPresetDescriptors (/tmp/npmcache.qz5Sl/_npx/595/lib/node_modules/@babel/core/lib/config/config-descriptors.js:101:10)
remote: at passPerPreset (/tmp/npmcache.qz5Sl/_npx/595/lib/node_modules/@babel/core/lib/config/config-descriptors.js:58:96) code: 'MODULE_NOT_FOUND' }
remote: npm ERR! code ELIFECYCLE
remote: npm ERR! errno 1


I think this answer attempts to explain the problem, but I really do not understand it, and there seems to be more going on as I can't even get it working with any of my packages not being preinstalled by NPM.

Upvotes: 1

Views: 262

Answers (1)

electric-skeptic
electric-skeptic

Reputation: 21

BabelJS staff Jordan Harband and Huáng Jùnliàng have helped me to understand why what I'd like to do isn't possible at this time, and there are two reasons:

1) Babel loses it's scope

2) Babel searches npx's current working directory instead of it's own

Upvotes: 1

Related Questions