使用 ts-node 执行打字稿文件时出错(将项目安装为全局模块后)
Error while executing typescript file with ts-node (after installing the project as global module)
使用 windows 8.1 64 位和纱线
我正在尝试创建一个全局模块,其中包含打字稿文件并 运行使用 ts-node
动态地对其进行编译(而不是将文件转换为 js)
示例项目位于 github
我想在命令行上执行 "gmt" 时 运行 它,所以我在 "package.json" 中添加了 "bin" 并提供了适当的值
并且还在主文件中添加了 shebang“#!/usr/bin/env ts-node”
但是当我通过 "yarn global add [directory-path]" 全局安装包后执行 "gmt" 时,我得到错误:-
C:\Users\gmaster>gmt
C:\Users\gmaster\AppData\Local\Yarn\Data\global\node_modules\gmt\bin\index.ts:6
let term: string = 'guys';
^
SyntaxError: Unexpected token :
at Module._compile (internal/modules/cjs/loader.js:720:23)
at Module._extensions..js (internal/modules/cjs/loader.js:787:10)
at Object.require.extensions.<computed> [as .ts] (C:\Users\gmaster\AppData\L
ocal\Yarn\Data\global\node_modules\ts-node\src\index.ts:485:14)
at Module.load (internal/modules/cjs/loader.js:643:32)
at Function.Module._load (internal/modules/cjs/loader.js:556:12)
at Function.Module.runMain (internal/modules/cjs/loader.js:839:10)
at Object.<anonymous> (C:\Users\gmaster\AppData\Local\Yarn\Data\global\node_
modules\ts-node\src\bin.ts:158:12)
at Module._compile (internal/modules/cjs/loader.js:776:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
at Module.load (internal/modules/cjs/loader.js:643:32)
我做错了什么吗?在网上搜索了几个小时。
我的目标是稍后将 ts 文件路径传递给 "gmt",以便 "gmt" 将动态导入我的打字稿文件并使用共享接口文件从中执行某些内容。但我什至无法使全局 ts 系统正常工作,:(
请帮忙。
也提一下有没有更好的方法可以达到我的目的。
非常感谢
node version 12.6.0
npm version 6.9.0
yarn version 1.16.0
ts-node version 8.4.1
tsc version 3.6.3
全局安装
更新:下述问题已在 ts-node@8.9.0
thanks to my report. --script-mode
now resolves symlinks before looking for the tsconfig.json
file. The correct shebang to use is #!/usr/bin/env ts-node-script
, which is now also documented 中修复。使用 npm -g install ts-node
确保您在全球范围内安装了最新版本。 (我不确定您 also/still 是否需要为此全局安装 TypeScript。如果是:npm -g install typescript
。)
已过时:我不确定这是否能解决您的问题,但我在 this issue. In short, many modules (including those of Node.js itself) require that you use the --esModuleInterop
option for the TypeScript compiler. (You can specify this in your tsconfig.json
file.) In order to be able to use your command from anywhere and not just from within your package directory, you have to use the undocumented --script-mode
option for ts-node
or the undocumented ts-node-script
command, which does the same.
中详细记录了我当前的方法
也就是说,你的shebang should either be #!/usr/bin/env -S ts-node --script-mode
or #!/usr/bin/env ts-node-script
. (The former uses -S
to pass arguments to the specified interpreter. If this doesn't work on your platform, you can try to hack around这个限制。)
如果你想使用包目录中的 bin
functionality of npm
(e.g. by running npm link
),上面的方法不起作用,因为 --script-mode
不(还?)遵循符号链接。要解决此问题,您可以在脚本的第一行使用 #!/usr/bin/env -S ts-node --project /usr/local/lib/node_modules/<your-project>/tsconfig.json
。但是,这并不理想,因为它破坏了平台独立性。我目前看到的唯一其他选择是改用 bash 脚本并从那里调用您的脚本。
使用 windows 8.1 64 位和纱线
我正在尝试创建一个全局模块,其中包含打字稿文件并 运行使用 ts-node
动态地对其进行编译(而不是将文件转换为 js)
示例项目位于 github
我想在命令行上执行 "gmt" 时 运行 它,所以我在 "package.json" 中添加了 "bin" 并提供了适当的值
并且还在主文件中添加了 shebang“#!/usr/bin/env ts-node”
但是当我通过 "yarn global add [directory-path]" 全局安装包后执行 "gmt" 时,我得到错误:-
C:\Users\gmaster>gmt
C:\Users\gmaster\AppData\Local\Yarn\Data\global\node_modules\gmt\bin\index.ts:6
let term: string = 'guys';
^
SyntaxError: Unexpected token :
at Module._compile (internal/modules/cjs/loader.js:720:23)
at Module._extensions..js (internal/modules/cjs/loader.js:787:10)
at Object.require.extensions.<computed> [as .ts] (C:\Users\gmaster\AppData\L
ocal\Yarn\Data\global\node_modules\ts-node\src\index.ts:485:14)
at Module.load (internal/modules/cjs/loader.js:643:32)
at Function.Module._load (internal/modules/cjs/loader.js:556:12)
at Function.Module.runMain (internal/modules/cjs/loader.js:839:10)
at Object.<anonymous> (C:\Users\gmaster\AppData\Local\Yarn\Data\global\node_
modules\ts-node\src\bin.ts:158:12)
at Module._compile (internal/modules/cjs/loader.js:776:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
at Module.load (internal/modules/cjs/loader.js:643:32)
我做错了什么吗?在网上搜索了几个小时。
我的目标是稍后将 ts 文件路径传递给 "gmt",以便 "gmt" 将动态导入我的打字稿文件并使用共享接口文件从中执行某些内容。但我什至无法使全局 ts 系统正常工作,:(
请帮忙。 也提一下有没有更好的方法可以达到我的目的。
非常感谢
node version 12.6.0
npm version 6.9.0
yarn version 1.16.0
ts-node version 8.4.1
tsc version 3.6.3
全局安装
更新:下述问题已在 ts-node@8.9.0
thanks to my report. --script-mode
now resolves symlinks before looking for the tsconfig.json
file. The correct shebang to use is #!/usr/bin/env ts-node-script
, which is now also documented 中修复。使用 npm -g install ts-node
确保您在全球范围内安装了最新版本。 (我不确定您 also/still 是否需要为此全局安装 TypeScript。如果是:npm -g install typescript
。)
已过时:我不确定这是否能解决您的问题,但我在 this issue. In short, many modules (including those of Node.js itself) require that you use the --esModuleInterop
option for the TypeScript compiler. (You can specify this in your tsconfig.json
file.) In order to be able to use your command from anywhere and not just from within your package directory, you have to use the undocumented --script-mode
option for ts-node
or the undocumented ts-node-script
command, which does the same.
也就是说,你的shebang should either be #!/usr/bin/env -S ts-node --script-mode
or #!/usr/bin/env ts-node-script
. (The former uses -S
to pass arguments to the specified interpreter. If this doesn't work on your platform, you can try to hack around这个限制。)
如果你想使用包目录中的 bin
functionality of npm
(e.g. by running npm link
),上面的方法不起作用,因为 --script-mode
不(还?)遵循符号链接。要解决此问题,您可以在脚本的第一行使用 #!/usr/bin/env -S ts-node --project /usr/local/lib/node_modules/<your-project>/tsconfig.json
。但是,这并不理想,因为它破坏了平台独立性。我目前看到的唯一其他选择是改用 bash 脚本并从那里调用您的脚本。