I installed typescript and the packages needed:
> npm i babylon
> npm i @babel/core
> npm i @babel/node
file main.ts starts with:
import * as babylon from 'babylon'
import { NodePath } from '@babel/node'
Compiling result:
error TS2307: Cannot find module '@babel/node' or its corresponding type declarations.
I tried to change .tsconfig json
{
...,
"skipLibCheck": true
}
and also added babel-load
package (found in Internet)
but tsc still reports this error
Meanwhile the package exists in package.json:
"dependencies": {
"@babel/core": "^7.25.9",
"@babel/generator": "^7.25.9",
"@babel/node": "^7.25.9",
"@babel/traverse": "^7.25.9",
"babel-loader": "^9.2.1",
"babylon": "^6.18.0"
}
The project doesn’t use babel.config
2
Answers
It seems that you should be importing
NodePath
from@babel/core
, not@babel/node
.Try:
The error was correctly stated as you cannot import from
@babel/node
because it is a Node CLI that also offers babel integration. Unlike a pure Node CLI, it allows you to run Javascript syntax that is not currently supported in Nodejs, while using Babel to transpile on the fly. Since it functions as a CLI, you don’t import it in your JavaScript files. It should be used as a developer tool to run scripts with transpilation@babel/core
is the main Babel package that transforms code parsed by@babel/parser
. This is the package to use if you need Babel to translate current Javascript syntax to natively supported syntax (ECMAScript 5), such asconst
/let
to var. This package can be used within the applications to perform code transformationsThe problem occurs because you are attempting to import
NodePath
via the CLI. To fix, only import any of Babel’s core functionality from the package.