您将如何调整以下脚本以允许电子主进程将 Typescript 与 ts-node 一起使用?
"scripts": {
"shell": "cross-env NODE_ENV=development electron ts-node ./app/main.ts"
}
Run Code Online (Sandbox Code Playgroud) 目前在node.js应用程序中使用typescript.我编译了我的应用程序,我正在使用typescript的路径映射功能.
'api/*'应该在根文件夹中查找.在开发工作中使用以下内容:
nodemon --watch src -e ts,tsx --exec ts-node -r tsconfig-paths/register --disableWarnings ./src/index.ts
Run Code Online (Sandbox Code Playgroud)
tsconfig-paths/register允许正确翻译需求,ts节点将正确执行/运行应用程序.现在,当我转向生产时,问题就出现了.在过去的制作中,我只是tsc在文件夹上运行,并将outDir(dist /)的内容移动到我的docker镜像中的/ app,然后运行node /app/index.js.这一直有效,直到我开始使用typescript的路径映射功能.现在我只是得到错误:
错误:找不到模块'api/module/path/here'
从这个github注释中,模块名称显然没有映射到输出编译的javascript.
我的tsconfig.json文件:
{
"compilerOptions": {
"target": "es2015",
"module": "commonjs",
"moduleResolution": "node",
"allowSyntheticDefaultImports": true,
"jsx": "react",
"allowJs": true,
"alwaysStrict": true,
"sourceMap": true,
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true,
"noImplicitReturns": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitAny": false,
"noImplicitThis": false,
"strictNullChecks": false,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"lib": ["es2017", "dom"],
"baseUrl": "src",
"outDir": "dist",
"types": [
"node"
],
"paths": …Run Code Online (Sandbox Code Playgroud) 我创建了一个基本的节点应用程序,其顶部带有打字稿。我正在使用 ts-node 来执行此操作,并且它与 nodemon 一起工作完全正常。但我现在需要将其移至服务器,我陷入困境。PM2一直显示错误。我已经浏览了 GitHub 和 StackOverflow 上的其他答案。这里没有任何帮助我。请帮忙。
\n\n我尝试使用 PM2 安装 typescript 和 ts-node。但它对我不起作用。我也尝试过直接运行文件,但没有成功。我现在不知道该如何解决这个问题。
\n\n "scripts": {\n "start": "nodemon -x ts-node src/server.ts"\n },\nRun Code Online (Sandbox Code Playgroud)\n\n使用简单的 npm run start 命令就可以正常工作
\n\nmadbo@DESKTOP-CS5UFKE MINGW64 /e/shailesh/nodejs/NodeType\n$ npm run start\n\n> NodeType@1.0.0 start E:\\shailesh\\nodejs\\NodeType\n> nodemon -x ts-node src/server.ts\n\n[nodemon] 1.18.5\n[nodemon] to restart at any time, enter `rs`\n[nodemon] watching: *.*\n[nodemon] starting `ts-node src/server.ts`\n24 Mar 22:33:23 - listening on port 3000\nMongoose default connection is open to mongodb://localhost:27017/todo \n\nRun Code Online (Sandbox Code Playgroud)\n\n到目前为止我所尝试的方法都不起作用 *(PM2已全局安装)*
\n\npm2 start ts-node -- …Run Code Online (Sandbox Code Playgroud) 在以下有 19 个赞成票的SO 评论中,用户对 Typescript 声明文件和使用说了以下内容typeRoots:
@Tom 它在与查找普通 .ts 文件相同的位置查找 .d.ts 文件:如 tsconfig.json 中指定的“文件”、“包含”和“排除”。我不建议为此目的使用 typeRoots:它用于外部类型模块(即 node_modules/@types)的位置,而不是单个 .d.ts 文件。
本质上,用户说不typeRoots应该用于单个 .d.ts 文件,但我不确定为什么会出现这种情况。这有充分的理由吗?
更让我困惑的是,该ts-node模块特别建议用户使用typeRootsin 属性tsconfig.json来声明全局类型定义。可以在此处找到概述此内容的文档。
所以问题是,我应该typeRoots按照文档概述的方式ts-node来声明全局类型定义吗?
tsconfig.json这是由 .net 自动生成的默认文件Next.js。
{
"compilerOptions": {
"target": "es5",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"strict": false,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext", // <=== THIS IS THE PROBLEM
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
},
"include": [
"next-env.d.ts",
"**/*.ts",
"**/*.tsx"
],
"exclude": [
"node_modules"
]
}
Run Code Online (Sandbox Code Playgroud)
我的问题是我在项目中运行一些脚本,并且我使用ts-node运行它们。
当我尝试运行有一些导入的脚本时,我遇到了各种各样的错误。
例如:
import fs from "fs";
console.log("HERE");
Run Code Online (Sandbox Code Playgroud)
我得到:Unexpected token export
我注意到如果进行以下更改tsconfig.json
"module": "esnext", // DELETE THIS LINE
"module": …Run Code Online (Sandbox Code Playgroud) 我有这个设置:
// tsconfig.json
{
"compilerOptions": {
"rootDir": "src",
...
}
...
//jest.config.ts
...
globals: {
'ts-jest': {
tsconfig: {
rootDir: '.'
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是当我运行 jest 时,我收到一个发出错误,指出打字稿文件需要位于根目录下,似乎这个“rootDir”编译器选项被忽略了。
我不希望我的 /test 或我的配置位于 /src 下,并且我不希望它被编译到 /lib 或被发出。
我也无法排除它,exclude因为这样它也排除了项目外部的 .ts 文件的类型,这使得使用 ts-node 非常困难。
我一直在尝试运行 eval 或从 repl 导入 esm 模块,但无法使其工作。我尝试了一些在互联网上找到的东西。例如:是否可以将 Typescript 导入到正在运行的 ts-node REPL 实例中?
不确定我错过了什么,我收到的错误是:
var a = await import("./test").then(x=>x)
//or
var a = await import("./test").then(x=>x)
//gives
//TypeError [ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING]: A dynamic import callback was not specified.
Run Code Online (Sandbox Code Playgroud)
import * as test from './src/test';
//works but then when I try to use 'test' it gives
//SyntaxError: Cannot use import statement inside the Node.js REPL, alternatively use dynamic import
Run Code Online (Sandbox Code Playgroud)
ts-node --esm -e "import * …Run Code Online (Sandbox Code Playgroud) 我有一个非常基本的 Express 服务器设置,使用 NodeJS/Express + Nodemon。我使用 WebStorm 作为我的 IDE。
运行调试/检查脚本时,我的应用程序抛出编译时错误。按照此处的说明并了解此处的帖子,我的(简化版)package.json如下所示:
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "nodemon src/app.ts",
"debug": "nodemon --inspect=127.0.0.1:9229 src/app.ts"
},
"devDependencies": {
"@types/express": "^4.17.9",
"@types/node": "^14.14.19",
"nodemon": "^2.0.6",
"ts-node": "^9.1.1",
"typescript": "^4.1.3"
}
Run Code Online (Sandbox Code Playgroud)
当我运行时npm run debug,我收到以下错误:
我还尝试了以下方法,结果都相同:
"debug": "nodemon --inspect src/app.ts""debug": "nodemon --inspect=0.0.0.0:9229 src/app.ts"任何想法为什么会发生这个错误?
该命令npm run dev不会生成错误。经过进一步调查,nodemon 在定位文件时会自动替换 ts-node .ts。反过来,ts-node建议通过注册 ts-node 并使用 node 命令运行服务器来进行调试。
如果您需要使用高级 node.js …
我无法在运行玩笑测试时附加 VS Code 调试器。我尝试使用launch.json找到的不同替代方案配置文件,但它们总是失败并显示以下错误消息,抱怨 node_modules 文件夹中的文件:
Debugger attached.
Waiting for the debugger to disconnect...
local\path\to\node_modules\.bin\jest:2
basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')")
^^^^^^^
SyntaxError: missing ) after argument list
at wrapSafe (internal/modules/cjs/loader.js:979:16)
at Module._compile (internal/modules/cjs/loader.js:1027:27)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
at internal/main/run_main_module.js:17:47
Run Code Online (Sandbox Code Playgroud)
我的package.json文件具有以下配置:
Debugger attached.
Waiting for the debugger to disconnect...
local\path\to\node_modules\.bin\jest:2
basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')")
^^^^^^^
SyntaxError: missing ) after argument list
at …Run Code Online (Sandbox Code Playgroud) 如何运行导入 css 模块的脚本?
我正在编写一个打字稿迁移脚本,我想通过ts-node. 理想情况下,我的代码库的组织方式应使脚本的依赖项永远不会接触 React 组件,但事实并非如此。该脚本最终导入 React 组件并因此导入 css 模块,因此ts-node失败,因为它不理解 css 模块:
RenderableInline.tsx(4,20): error TS2307: Cannot find module './styles/RenderableInline.module.scss' or its corresponding type declarations.
Run Code Online (Sandbox Code Playgroud)
只有 webpack 知道如何构建 css 模块,因为我已经通过css-loader.
我发现的唯一先例是 jest,它有一些用于模拟 css 模块的配置选项,因此它可以毫无错误地导入这些文件: https: //jestjs.io/docs/webpack。
如何运行依赖于 css 模块的 typescript 脚本?有什么办法可以通过 做到这一点ts-node吗?或者webpack有一些脚本运行功能吗?
ts-node ×10
typescript ×9
node.js ×5
javascript ×2
jestjs ×2
ts-jest ×2
electron ×1
next.js ×1
node-modules ×1
nodemon ×1
pm2 ×1
tsconfig ×1
webpack ×1
webstorm ×1