Reputation: 1073
I'm working on a small Node CLI that uses Babel to transpile my code. I am not currently using Webpack or any bundler.
The use-case for this app is to download it from a registry (e.g., npm) and then use it directly.
I have recently added @babel/runtime
as a dependency to the app (it was a silent dependency before, needed for other packages, but only those that are listed as devDependencies
and so was included in the production bundle).
My question ultimately is: is this the best way to handle this situation? Ideally the plugin-transform-runtime
would transpile the code and get out of the way so that babel wasn't needed as a dependency at all.
Additional context for the state of things before I added @babel/runtime
as a dependency, this is what I would see when I tried to utilize the CLI:
stephen@Stephens-MBP-2 code % volta install note-mgr-cli@alpha
success: installed [email protected] with executables: nom
stephen@Stephens-MBP-2 code % nom init
internal/modules/cjs/loader.js:968
throw err;
^
Error: Cannot find module '@babel/runtime/helpers/interopRequireDefault'
Require stack:
- /Users/stephen/.volta/tools/image/packages/note-mgr-cli/0.0.4-2/dist/index.js
- /Users/stephen/.volta/tools/image/packages/note-mgr-cli/0.0.4-2/index.js
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:965:15)
at Function.Module._load (internal/modules/cjs/loader.js:841:27)
at Module.require (internal/modules/cjs/loader.js:1025:19)
at require (internal/modules/cjs/helpers.js:72:18)
at Object.<anonymous> (/Users/stephen/.volta/tools/image/packages/note-mgr-cli/0.0.4-2/dist/index.js:3:30)
at Module._compile (internal/modules/cjs/loader.js:1137:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10)
at Module.load (internal/modules/cjs/loader.js:985:32)
at Function.Module._load (internal/modules/cjs/loader.js:878:14)
at Module.require (internal/modules/cjs/loader.js:1025:19) {
code: 'MODULE_NOT_FOUND',
requireStack: [
'/Users/stephen/.volta/tools/image/packages/note-mgr-cli/0.0.4-2/dist/index.js',
'/Users/stephen/.volta/tools/image/packages/note-mgr-cli/0.0.4-2/index.js'
]
}
Inspecting the file, dist/index.js
, it's clear why this would be a problem (it references @babel/runtime
, but @babel/runtime
was not present in the node_modules
):
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Upvotes: 1
Views: 1593
Reputation: 1073
The short answer is yes: @babel/runtime
(and @babel/plugin-transform-runtime
) is intended to be treated as a dependency.
From the Babel docs
Usage
This is meant to be used as a runtime dependency along with the Babel plugin @babel/plugin-transform-runtime. Please check out the documentation in that package for usage.)
This is also spelled out in the installation directive which specifies --save
(unlike most other @babel
packages which are --save-dev
):
npm install --save @babel/runtime
Upvotes: 4