SyntaxError: Missing initializer in const declaration (nodejs+typescript+babel)
See original GitHub issueBug Report
Current Behavior
I am trying to use babel with typescript in nodejs environment with .js
files.
I was following https://babeljs.io/docs/en/babel-preset-typescript
I am getting the following SyntaxError
:
SyntaxError: Missing initializer in const declaration at createScript (vm.js:80:10) at Object.runInThisContext (vm.js:139:10) at Module._compile (module.js:617:28) at Object.Module._extensions…js (module.js:664:10) at Module.load (module.js:566:32) at tryModuleLoad (module.js:506:12) at Function.Module._load (module.js:498:3) at Function.Module.runMain (module.js:694:10) at startup (bootstrap_node.js:204:16) at bootstrap_node.js:625:3
Input Code
require("@babel/core").transform("code", {
presets: ["@babel/preset-typescript"],
});
const x: number = 0;
console.log(x);
Expected behavior/code
The code should be compiled by babel.
Babel Configuration (.babelrc, package.json, cli command)
require("@babel/core").transform("code", {
presets: ["@babel/preset-typescript"],
});
Environment
- Babel version(s): 7
- Node/npm version: 8.15.0
- OS: Windows 7
- Monorepo: lerna
- How you are using Babel:
register
Issue Analytics
- State:
- Created 5 years ago
- Comments:7 (6 by maintainers)
It should be
server.ts
: the TypeScript preset only runs on typescript files. If you want to call it .js you need to use@babel/plugin-transform-typescript
instead.same issue with changing the files to
.ts
.but using
@babel/plugin-transform-typescript
with.js
files actually works.Thanks A lot !