microsoft / dts-gen

dts-gen creates starter TypeScript definition files for any module or library.
MIT License
2.43k stars 102 forks source link

Unexpected token [ #59

Open ghost opened 7 years ago

ghost commented 7 years ago

dts-gen -m yargs C:\Users\melhamod\AppData\Roaming\npm\node_modules\dts-gen\bin\lib\definitely-typed.js:40 for (const [name, text] of files) { ^

SyntaxError: Unexpected token [ at exports.runInThisContext (vm.js:53:16) at Module._compile (module.js:373:25) at Object.Module._extensions..js (module.js:416:10) at Module.load (module.js:343:32) at Function.Module._load (module.js:300:12) at Module.require (module.js:353:17) at require (internal/module.js:12:17) at Object. (C:\Users\melhamod\AppData\Roaming\npm\node_modules\dts-gen\bin\lib\run.js:8:28) at Module._compile (module.js:409:26) at Object.Module._extensions..js (module.js:416:10)

uglycoyote commented 7 years ago

also experiencing this; first time trying dts-gen. it's obviously not anything to do with the particular library I'm trying to parse. Giving a dummy name or just invoking "dts-gen" without any arguments causes this failure.

zamnuts commented 7 years ago

TL;DR: Not a bug, use Node v6 or greater as described in engines node of package.json (introduced in git rev faa66e6)


for (const [name, text] of files) uses two ES6 features:

The particular error being cited refers to the use of [ following the const reserved word, meaning the problem is in regards to the destructuring feature. I'm guessing you're using Node.js v4, where this feature does not exist.

The solution is to use Node.js v6.0.0 or greater when generating, e.g.:

# install and use node v6, using node version manager...
nvm install v6
nvm use v6

# install dst-gen and your module globally under your v6 context
npm install dts-gen -g
npm install yargs -g # alternatively `npm link` will work if you haven't published yet

# generate
dts-gen -m yargs

# switch back for development
nvm use v4

Remembering your old version and switching back is straight-forward too:

currentVer=$(nvm current)
nvm use v6
npm install yargs -g
dts-gen -m yargs
nvm use "$currentVer"
uglycoyote commented 7 years ago

@zamnuts I see.. thanks. Though I'm a little puzzled why, if the package.json contains a line that says which node version must be used, why don't get I get a big obvious error message saying "This project cannot be built/run with your old node version"? I don't ever recall seeing a warning like that. My output looked identical to mndhamod's, which gives you no hints that you might be using obsolete software.

Perhaps it needs an "engine-strict" to prevent being run from older node versions that don't have destructuring support.