简体   繁体   English

如何在 ts-node 中使用项目依赖

[英]How to use project dependencies with ts-node

I am currently debugging a project that uses ts-node to run locally.我目前正在调试一个使用ts-node在本地运行的项目。 Do I have to install ts-node globally for this?我是否必须为此全局安装ts-node

I did install it globally, but now i am seeing typescript errors that stems from diverging typescript versions.我确实在全球范围内安装了它,但现在我看到了由于打字稿版本不同而导致的打字稿错误。 I fixed it by setting it to the same version.我通过将其设置为相同版本来修复它。

Is there a way to use my project dependencies in my CLI to run the project?有没有办法在我的 CLI 中使用我的项目依赖项来运行项目? Or do I HAVE to install things globally?还是我必须在全球范围内安装东西?

Since npm@5.2.0 , npm ships with the npx package, which lets you run commands from the node_modules/.bin directory in your current project.由于npm@5.2.0 ,NPM附带npx包,它可以让你从运行命令node_modules/.bin在当前项目目录。 For example: npx ts-node -v .例如: npx ts-node -v

If for whatever reason you have an older version of npm, you can install this package globally with npm install -g npx .如果由于某种原因你有一个旧版本的 npm,你可以使用npm install -g npx全局安装这个包。

You can use npx <package-name> also for packages that aren't installed yet in your project (see its options ).您也可以将npx <package-name>用于项目中尚未安装的软件包(请参阅其选项)。

Alternatively, you can put the following into your package.json :或者,您可以将以下内容放入您的package.json

"scripts": {
    "start": "ts-node code/start.ts",
}

Commands in the scripts field also use the local version of a package. scripts字段中的命令也使用包的本地版本。

By default, the node_modules folder has a .bin subdirectory.默认情况下, node_modules文件夹有一个.bin子目录。 You can run the local version of ts-node while in the project directory.您可以在项目目录中运行本地版本的ts-node

Here's what you might type into your shell以下是您可能在 shell 中输入的内容

cd /path/to/project
./node_modules/.bin/ts-node myFile.ts

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM