ts-node 执行带有模块导入和模块定义的打字稿

问题描述 投票:0回答:4

我尝试通过打字稿初始化数据库。

我的 ts 是这样的:

import { User, UserRole } from '../entity/User';
import crypto from 'crypto';
import {dbManager, pwhash } from '..';

async function inituser() 
{
    const user = new User();
    user.email = '[email protected]';
    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);

当我尝试通过 ts-node 运行脚本时:

npx ts-node db/initializers/inituser.ts

有错误:

SyntaxError: Cannot use import statement outside a module
    at wrapSafe (internal/modules/cjs/loader.js:1167:16)
    at Module._compile (internal/modules/cjs/loader.js:1215:27)
    at Module.m._compile (/usr/lib/node_modules/ts-node/src/index.ts:858:23)
    at Module._extensions..js (internal/modules/cjs/loader.js:1272:10)
    at Object.require.extensions.<computed> [as .ts] (/usr/lib/node_modules/ts-node/src/index.ts:861:12)
    at Module.load (internal/modules/cjs/loader.js:1100:32)
    at Function.Module._load (internal/modules/cjs/loader.js:962:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
    at main (/usr/lib/node_modules/ts-node/src/bin.ts:227:14)
    at Object.<anonymous> (/usr/lib/node_modules/ts-node/src/bin.ts:513:3)
root@MEDAIHILW237:/mnt/c/workgit/projeny# ts-node db/initializers/inituser.ts
TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /mnt/c/workgit/projeny/db/initializers/inituser.ts
    at Loader.defaultGetFormat [as _getFormat] (internal/modules/esm/get_format.js:65:15)
    at Loader.getFormat (internal/modules/esm/loader.js:101:42)
    at Loader.getModuleJob (internal/modules/esm/loader.js:230:31)
    at Loader.import (internal/modules/esm/loader.js:164:17)
    at Object.loadESM (internal/process/esm_loader.js:68:5)
root@MEDAIHILW237:/mnt/c/workgit/projeny# ts-node db/initializers/inituser
TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /mnt/c/workgit/projeny/db/initializers/inituser.ts
    at Loader.defaultGetFormat [as _getFormat] (internal/modules/esm/get_format.js:65:15)
    at Loader.getFormat (internal/modules/esm/loader.js:101:42)
    at Loader.getModuleJob (internal/modules/esm/loader.js:230:31)
    at Loader.import (internal/modules/esm/loader.js:164:17)
    at Object.loadESM (internal/process/esm_loader.js:68:5)

我在 package.json 文件中添加“类型”:“模块”。

{
  "name": "typescript-test",
  "version": "1.0.0",
  "type": "module",
...
}

克服

的错误
(node:3854) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
typescript package.json tsconfig ts-node
4个回答
37
投票

错误

Warning: To load an ES module, set "type": "module" in the package.json or
是由ts-node中的以下错误引起的:https://github.com/TypeStrong/ts-node/issues/935

Bug已关闭,有解决方案,但仍然开放:https://github.com/TypeStrong/ts-node/issues/1007

我不需要

"type": "module"
package.json
.

tsconfig.json
中,我使用的是
"module": "commonjs"
而不是
"module": "es6",

我注意到这是技术债务未决,ts-node 问题 #1007 已解决。


3
投票

当 ts-node 在项目中运行时,它使用本地的 tsconfig.json,通常不会设置为执行构建。

但是您可以添加一个专门用于 ts-node 的部分,覆盖这些设置。告诉它为 esm 构建对我有用:

{
  "compilerOptions": {
    ...
  },
  "ts-node": {
    "esm": true,
    "compilerOptions": {
      "module": "nodenext",
    }
  }
}

2
投票

也许这有帮助:

node --no-warnings --loader ts-node/esm file.ts

-1
投票

只需添加

-O '{\"module\": \"commonjs\"}'

npx ts-node -O '{\"module\": \"commonjs\"}' db/initializers/inituser.ts

而且不需要在package.json中设置

"type": "module"

© www.soinside.com 2019 - 2024. All rights reserved.