Error: Cannot find module '@babel/preset-env' – @babel/core resolver
0 reputation · 17 Sept 2021, 04:21 UTC
0 reputation · 17 Sept 2021, 04:21 UTC
The goal is to ensure Babel can locate and load a preset declared in the configuration so that the transformation pipeline proceeds without aborting due to a missing‑module error.
Currently the resolver treats each preset as an independent Node module requirement; there is no standardized way for a preset to declare dependencies on other presets or plugins that must be present beforehand. This leaves an open design question about whether Babel should adopt a dependency‑declaration mechanism that could influence load order and potentially prevent missing‑module errors caused by omitted transitive dependencies.
- Should presets be allowed to list required peer presets/plugins that Babel automatically resolves before loading the preset itself?
- Would introducing a preset dependency field alter the semantics of the existing left‑to‑right plugin/preset ordering guarantee?
- How would such a mechanism interact with version resolution and lockfiles without breaking backward compatibility?
The error occurs when @babel/core tries to require '@babel/preset-env' but the package is absent from the project's node_modules or cannot be resolved due to hoisting or version mismatch.
# npm
npm install --save-dev @babel/preset-env
# yarn
yarn add -D @babel/preset-env
# pnpm
pnpm add -D @babel/preset-env
ls -la node_modules/@babel/preset-env/package.json
npm ls @babel/core @babel/preset-env
rm -rf node_modules package-lock.json yarn.lock pnpm-lock.yaml
npm install # or yarn install / pnpm install
node -e "console.log(require.resolve('@babel/preset-env'))"
Should print an absolute path ending in …/lib/index.js.
echo "const x = () => 1;" > test.js
npx babel test.js --presets=@babel/preset-env
If output shows transformed code without error, the preset loads correctly.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.