我有一个用打字稿编写的简单节点服务器。我的package.json配置为:
"scripts": {
"build": "tsc",
"dev": "nodemon --watch src/**/* -e ts,json --exec ts-node ./src/server.ts",
"debug": "nodemon --verbose --watch src/**/* -e ts,json --exec ts-node --inspect ./src/server.ts"
},
Run Code Online (Sandbox Code Playgroud)
当我运行时,npm run devnodemon将启动服务器,并在进行任何更改后重新启动它。
[02/28/18 20:45:53] npm run dev
> pq-api@1.0.0 dev C:\Users\joe\pq\pq-api
> nodemon --watch src/**/* -e ts,json --exec ts-node ./src/server.ts
[nodemon] 1.15.1
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: src/**/*
[nodemon] starting `ts-node ./src/server.ts`
initializing config to development
info: PQ-API running on port 3000
Run Code Online (Sandbox Code Playgroud)
但是,当我运行npm run debug …
我遇到了解决打字稿中非相对模块的问题。并尝试等等,但它不起作用baseUrl......paths
tsconfig.json
\n\n{\n "compilerOptions": {\n "allowJs": true,\n "baseUrl": ".",\n "esModuleInterop": true,\n "module": "commonjs",\n "sourceMap": true,\n "strict": true,\n "target": "esnext",\n "typeRoots": ["node_modules/@types"]\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n\n项目目录:
\n\nroot\n \xe2\x94\x9c\xe2\x94\x80src\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 APIs\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 config\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 constants\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 middlewares\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 models\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 routes\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 services\n - foo.ts \n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 utils\n \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 app.ts\nRun Code Online (Sandbox Code Playgroud)\n\n在app.ts中
\n\nimport foo from \'src/services/foo\'\nRun Code Online (Sandbox Code Playgroud)\n\n并与 一起运行ts-node src/app.ts。
但出现错误:
\n\nCannot find module \'src/services/foo\'\nRun Code Online (Sandbox Code Playgroud)\n 请注意:这个问题是因为在运行我的装饰方法时使用了 GraphQL 解析器。这意味着 的范围this是undefined。但是,该问题的基础知识对于遇到装饰器问题的任何人都很有用。
这是我想要使用的基本装饰器(我的有更多代码):
const someDecorator = (argPassed: any) => {
return (target: any, propertyKey: string, descriptor: PropertyDescriptor) => {
const originalMethod = descriptor.value;
// DO stuff here...
console.log(argPassed);
// Wrapping the original method
descriptor.value = (...args: any[]) => {
const result = originalMethod.apply(this, args);
return result;
};
};
};
Run Code Online (Sandbox Code Playgroud)
我在装饰器中使用箭头函数,这是我可以让它返回某种范围的唯一方法,尽管与普通this范围不同。
这是我正在使用的类和我正在装饰的方法:
class SomeClass {
constructor() {
}
@someDecorator('Passing this in...')
public doingSomething(argPassed: string) {
console.log(this); // Returns: { default: SomeClass { otherMethodInMyClass: …Run Code Online (Sandbox Code Playgroud) 我正在尝试将 express 导入项目,在开发过程中,它停止工作,我重置了配置等。无法使其工作,无法导入任何模块。
npx tsc src/server.ts
src/server.ts:1:8 - error TS1259: Module '"/home/fpc-ubut/Git/nlw2/server/node_modules/@types/express/index"' can only be default-imported using the 'esModuleInterop' flag
1 import express from 'express';
Run Code Online (Sandbox Code Playgroud)
src/database/migrations/00_create_users.ts:1:8 - error TS1259: Module '"/home/fpc-ubut/Git/nlw2/server/node_modules/knex/types/index"' can only be default-imported using the 'esModuleInterop' flag
1 import Knex from 'knex'
Run Code Online (Sandbox Code Playgroud)
src/server.ts:1:8 - error TS1259: Module '"path"' can only be default-imported using the 'esModuleInterop' flag
1 import path from 'path'
Run Code Online (Sandbox Code Playgroud)
环境操作系统:Ubuntu 20.04.1 LTS
节点:v12.18.1
包:
"@types/express": "^4.17.6",
"@types/node": "^14.0.27",
"ts-node": "^8.10.2",
"ts-node-dev": "^1.0.0-pre.44",
"typescript": "^3.9.3"
"express": "^4.17.1", …Run Code Online (Sandbox Code Playgroud) 我尝试通过打字稿初始化数据库库。
我的 ts 是这样的:
import { User, UserRole } from '../entity/User';
import crypto from 'crypto';
import {dbManager, pwhash } from '..';
async function inituser()
{
const user = new User();
user.email = 'sheng.lu@mq.edu.au';
user.own_organization = []
user.salt = crypto.randomBytes(16).toString('hex');
user.password = pwhash("password", user.salt);
user.role = UserRole.admin;
await dbManager.save(user);
const duser = await dbManager.findOne(User);
return duser;
}
const duser = inituser();
console.log("Loaded users: ", duser);
Run Code Online (Sandbox Code Playgroud)
当我尝试通过 ts-node 运行脚本时:
npx ts-node db/initializers/inituser.ts
Run Code Online (Sandbox Code Playgroud)
有错误:
SyntaxError: Cannot use import statement outside a module
at …Run Code Online (Sandbox Code Playgroud) 有没有办法在调用 ts-node 时将选项传递给 node?我正在尝试在 node 中使用一个实验性功能,如果它可以与 ts-node 一起使用,它会膨胀。
这就是我目前正在做的事情:
ts-node ./src/utils/repl.ts -- --experimental-repl-await
Run Code Online (Sandbox Code Playgroud) 我们有一个像这样的代码库设置:
-A
--utils
---index.ts
--index.ts
--tsconfig.json
-B
--utils
---index.ts
--index.ts
--tsconfig.json
-tsconfig.json
Run Code Online (Sandbox Code Playgroud)
我们的根tsconfig.json包含以下内容:
"paths": {
"A/*": ["A/*"],
"B/*": ["B/*"],
},
Run Code Online (Sandbox Code Playgroud)
每个包tsconfig.json包含以下内容:
"paths": {
"utils/*": ["./utils/*"],
},
Run Code Online (Sandbox Code Playgroud)
这适用于我们当前的目的,因为我们目前只使用 TS 进行类型检查,但我们实际上使用 Babel 生成构建,它alias在各种.babelrc文件的属性中具有类似的设置。
我们希望开始使用ts-node(或等效的)运行代码,但在运行时遇到了问题,TS 不知道如何解析模块。例如:
// A/index.ts
import { someUtil } from 'utils'
export const someFunc() => someUtil();
Run Code Online (Sandbox Code Playgroud)
// B/index.ts
import { someFunc } from 'A';
Run Code Online (Sandbox Code Playgroud)
当我们运行时,npx ts-node ./B/index.ts我们收到一条错误消息,提示“无法找到模块 'utils'。
我们知道我们可以通过将所有内容提升到 root 来实现这一点tsconfig.json,但是import { someUtil } from …
我成功地将ts-node其转换为 CommonJS 模块。我用的是官方的:docs
我还想按照官方esm 文档使用 esm 模块,但不幸的是没有成功。我不断收到的错误是:CustomError: Cannot find module '/module/next/to/index/ts/ModuleName' imported from /Users/mainuser/angular_apps/typescript_test/index.ts
这是tsconfig.json
{
"compilerOptions": {
"target": "ES2015",
"module": "ES2020",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true
},
"ts-node": {
"esm": true
}
}
Run Code Online (Sandbox Code Playgroud)
这是package.json的竞赛:
{
"name": "typescript_test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {},
"devDependencies": {
"ts-node": "^10.7.0",
"typescript": "^4.6.3"
}, …Run Code Online (Sandbox Code Playgroud) 我有一个使用未知节点和 npm 版本构建的节点应用程序。有没有办法猜测版本,或者至少是版本范围package-lock.json?
我确实有"lockfileVersion": 1,,这意味着 npm v5 或 v6。有什么办法可以获得更细粒度吗?
我需要它的原因是,我在运行时遇到一堆这样的错误ts-node,除非我删除并重建package-lock.json。由于各种原因,我宁愿不这样做。
ts.Debug.assert(typeof typeReferenceDirectiveName === "string", "Non-string value passed to `ts.resolveTypeReferenceDirective`, likely by a wrapping package working with an outdated `resolveTypeReferenceDirectives` signature. This is probably not a problem in TS itself.");
Run Code Online (Sandbox Code Playgroud) 如何将 VSCode 调试器附加到ts-node使用环境变量的命令,如下所示:
包.json:
{
"runMyScript": "ts-node -r dotenv/config --transpile-only tasks/migration/myScript.ts dotenv_config_path=./local.env"
}
Run Code Online (Sandbox Code Playgroud)
我尝试--respawn --inspect=4000使用下面的 launch.json 将标志添加到上述命令,但它不起作用:
启动.json:
{"configurations": [
{
"name": "RUN attach to DEV",
"type": "node",
"port": 4000,
"request": "attach",
"trace": true,
"skipFiles": ["<node_internals>/**"],
"restart": true
},
]}
Run Code Online (Sandbox Code Playgroud) ts-node ×10
typescript ×9
node.js ×5
debugging ×2
nodemon ×1
npm ×1
package.json ×1
tsconfig ×1