question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SyntaxError: Missing initializer in const declaration (nodejs+typescript+babel)

See original GitHub issue

Bug 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:closed
  • Created 5 years ago
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
nicolo-ribaudocommented, Feb 28, 2019

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.

0reactions
jony89commented, Feb 28, 2019

same issue with changing the files to .ts.

const x: number = 0; ^

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 Module._extensions…js (module.js:664:10) at Object.newLoader [as .js] (…\node_modules\pirates\lib\index.js:104:7) at Module.load (module.js:566:32) at tryModuleLoad (module.js:506:12) at Function.Module._load (module.js:498:3) at Module.require (module.js:597:17) at require (internal/module.js:11:18)

but using @babel/plugin-transform-typescript with .js files actually works.

Thanks A lot !

Read more comments on GitHub >

github_iconTop Results From Across the Web

SyntaxError: missing = in const declaration - JavaScript | MDN
In JavaScript, constants are declared using the const keyword. An initializer for a constant is required; that is, you must specify its value...
Read more >
Missing initializer in const declaration Error in JS | bobbyhadz
The "Missing initializer in const declaration" error occurs when a variable is declared using const , but its value is not initialized on...
Read more >
Typescript - Missing initializer in const declaration
You need to declare an interface for manufacturers , as TypeScript won't be able infer the properties for typechecking if you use any...
Read more >
SyntaxError: missing = in const declaration - JavaScript
In JavaScript, constants are declared using the const keyword. An initializer for a constant is required; that is, you must specify its value...
Read more >
JavaScript SyntaxError - Missing = in const declaration
This JavaScript exception missing = in const declaration occurs if a const is declared and value is not provided(like const ABC_DEF;). Need to ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found