ts node

来源:4-3 初始化项目 - TypeScript library starter

wiekern

2019-06-26

npm install failed when using typescript-library-starter to init the project

C:\Users\xxx\Desktop\ts-axios>npm install
npm WARN deprecated @types/handlebars@4.1.0: This is a stub types definition. handlebars provides its own type definitions, so you do not need this installed.

> --libraryname--@0.0.0 postinstall C:\Users\xxx\Desktop\ts-axios
> ts-node tools/init

'ts-node' is not recognized as an internal or external command,
operable program or batch file.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! --libraryname--@0.0.0 postinstall: `ts-node tools/init`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the --libraryname--@0.0.0 postinstall script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\xxx\AppData\Roaming\npm-cache\_logs\2019-06-25T18_32_02_015Z-debug.log

attempt to solve it by chaninng the version of ts-node to the lastest 8.3.0, then new error appears as below

  const pkg = JSON.parse(readFileSync(jsonPackage) as any)
                   ^
SyntaxError: Unexpected token Y in JSON at position 246
    at JSON.parse (<anonymous>)
    at finalize (C:\Users\xxx\Desktop\ts-axios\tools\init.ts:264:20)
    at setupLibrary (C:\Users\xxx\Desktop\ts-axios\tools\init.ts:183:3)
    at C:\Users\xxx\Desktop\ts-axios\tools\init.ts:124:7
    at C:\Users\xxx\Desktop\ts-axios\node_modules\prompt\lib\prompt.js:336:32
    at C:\Users\xxx\Desktop\ts-axios\node_modules\utile\node_modules\async\lib\async.js:154:25
    at assembler (C:\Users\YafeiYan\Desktop\ts-axios\node_modules\prompt\lib\prompt.js:333:9)
    at C:\Users\xxx\Desktop\ts-axios\node_modules\prompt\lib\prompt.js:342:32
    at C:\Users\xxx\Desktop\ts-axios\node_modules\prompt\lib\prompt.js:625:5
    at Interface.onLine (C:\Users\xxx\Desktop\ts-axios\node_modules\read\lib\read.js:111:5)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! --libraryname--@0.0.0 postinstall: `ts-node tools/init`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the --libraryname--@0.0.0 postinstall script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\xxx\AppData\Roaming\npm-cache\_logs\2019-06-25T18_42_04_302Z-debug.log
写回答

5回答

wiekern

提问者

2019-06-28

实锤了!看了 typescript-library-starter 源码,package.json 中的 "author" 字段是通过 `git config user.name` 获取。之前失败是因为得到的用户名是带前缀“AzureAD\”,这里的"\"放入到 package.json 中导致 json 文件不合法,从而初始化会失败。下图是我设置完全局 git 用户名之后,初始化成功!

//img.mukewang.com/szimg/5d14f4fa0001175e11630371.jpg

//img.mukewang.com/szimg/5d14f5e10001d13912440859.jpg


0
1
ustbhuangyi
赞从根源上去解决问题的思路
2019-06-28
共1条回复

wiekern

提问者

2019-06-28

node: 8.15.1

npm: 6.4.1

OS: Windows 10

account: administrator (本地账号)

在新环境下重新使用 typescript-library-starter 初始化项目成功!使用 BeyondCompare 比较了两个项目文件,发现如下异同

1.

//img.mukewang.com/szimg/5d14ee4f00018ab813920098.jpg 

2. 某个配置文件和 git 用户名相关字段不同,新项目为空,老项目因为用户名前缀带了微软 “AzureAD\”这样的前缀,因为用的是hotmail邮箱登陆的用户,并不是administrator这样的本地用户,而且在administrator环境下项目初始化成功的,所以我猜是因为这个前缀导致项目初始化过程中出问题。

0
1
wiekern
上面图中左为新项目(成功初始化的),右边是老项目(失败的)
2019-06-28
共1条回复

ustbhuangyi

2019-06-26

你的 node 和 npm 版本是多少?

0
3
wiekern
回复
ustbhuangyi
步骤完全按你的视频里来的,项目运行没啥问题。 但是还有问题,我加了新的依赖再npm install时,又报错。 > ts-node tools/init Error: Cannot find module 'C:\Users\YafeiYan\Desktop\ts-axios\tools\init' at Function.Module._resolveFilename (module.js:548:15) at Function.Module._load (module.js:475:25) at Function.Module.runMain (module.js:694:10) at Object. (C:\Users\YafeiYan\Desktop\ts-axios\node_modules\ts-node\src\bin.ts:157:12) at Module._compile (module.js:653:30) 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)
2019-06-27
共3条回复

wiekern

提问者

2019-06-26

//img.mukewang.com/szimg/5d12986600015b3302820579.jpg虽然出错,但是目录结构看起来是完整的。

0
0

wiekern

提问者

2019-06-26

```

> ts-node tools/init

Hi! You're almost ready to make the next great TypeScript library.

Would you like it to be called "typescript-axios"? [Yes/No]:  yes


Thanks for the info. The last few changes are being made... hang tight!



AzureAD\xxx

xxx@gmail.com

Removed

.git

.all-contributorsrc

.gitattributes

tools/init.ts



Modified

LICENSE

package.json

rollup.config.ts

test/library.test.ts

tools/gh-pages-publish.ts



Renamed

src/library.ts => src/typescript-axios.ts

test/library.test.ts => test/typescript-axios.test.ts



Finalizing

Initialized empty Git repository in C:/Users/xxx/Desktop/typescript-axios/.git/


C:\Users\xxx\Desktop\typescript-axios\tools\init.ts:264

  const pkg = JSON.parse(readFileSync(jsonPackage) as any)

                   ^

SyntaxError: Unexpected token Y in JSON at position 289

    at JSON.parse (<anonymous>)

    at finalize (C:\Users\xxx\Desktop\typescript-axios\tools\init.ts:264:20)

    at setupLibrary (C:\Users\xxx\Desktop\typescript-axios\tools\init.ts:183:3)

    at C:\Users\xxx\Desktop\typescript-axios\tools\init.ts:124:7

    at C:\Users\xxx\Desktop\typescript-axios\node_modules\prompt\lib\prompt.js:336:32

    at C:\Users\xxx\Desktop\typescript-axios\node_modules\utile\node_modules\async\lib\async.js:154:25

    at assembler (C:\Users\xxx\Desktop\typescript-axios\node_modules\prompt\lib\prompt.js:333:9)

    at C:\Users\xxx\Desktop\typescript-axios\node_modules\prompt\lib\prompt.js:342:32

    at C:\Users\xxx\Desktop\typescript-axios\node_modules\prompt\lib\prompt.js:625:5

    at Interface.onLine (C:\Users\xxx\Desktop\typescript-axios\node_modules\read\lib\read.js:111:5)

npm ERR! code ELIFECYCLE

npm ERR! errno 1

npm ERR! --libraryname--@0.0.0 postinstall: `ts-node tools/init`

npm ERR! Exit status 1

npm ERR!

npm ERR! Failed at the --libraryname--@0.0.0 postinstall script.

npm ERR! This is probably not a problem with npm. There is likely additional logging output above.


npm ERR! A complete log of this run can be found in:

npm ERR!     C:\Users\xxx\AppData\Roaming\npm-cache\_logs\2019-06-25T19_52_31_495Z-debug.log

```

重新初始化一次项目,老问题。


0
0

下一代前端开发语言 TypeScript从零重构axios

课程从零开始重构功能完整的JS库,是学习造轮子的不二之选!

2629 学习 · 877 问题

查看课程