简体   繁体   English

将 esm 模块与 ts-node 一起使用

[英]using esm modules with ts-node

I successfully ran ts-node that transpiles to CommonJS modules.我成功运行了转译为 CommonJS 模块ts-node I used the official: docs我用的是官方: docs

I also wanted to use esm modules following the officialesm docs , but was unfortunately unsuccessful.我也想按照官方esm 文档使用 esm 模块,但不幸的是没有成功。 The error I keep getting is: CustomError: Cannot find module '/module/next/to/index/ts/ModuleName' imported from /Users/mainuser/angular_apps/typescript_test/index.ts我不断收到的错误是: CustomError: Cannot find module '/module/next/to/index/ts/ModuleName' imported from /Users/mainuser/angular_apps/typescript_test/index.ts

This is tsconfig.json这是tsconfig.json

{
    "compilerOptions": {
      "target": "ES2015",                                 
      "module": "ES2020",                               
      "esModuleInterop": true,                             
      "forceConsistentCasingInFileNames": true,            
      "strict": true,                                      
         "skipLibCheck": true                                
    },
    "ts-node": {
      "esm": true
    }
  }
  

and this is the contest of package.json :这是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"
  },
  "type": "module"

}

I run ts-node with: ./node_modules/.bin/ts-node index.ts while being in the project root.我在项目根目录中运行 ts-node: ./node_modules/.bin/ts-node index.ts /.bin/ts-node index.ts。

What am I missing?我错过了什么?

This is my index.ts code:这是我的 index.ts 代码:

import { SmokeTest } from "./SmokeTest";
SmokeTest.Log();

And this is the SmokeTest.ts file contents.这是 SmokeTest.ts 文件内容。 Both are at the same fs level:两者都处于相同的 fs 级别:

export module SmokeTest{
    export function Log(){
        console.log("Smoke test running");  
    }
}

I am running node v14.19.1 and ts-node v10.7.0我正在运行节点 v14.19.1 和 ts-node v10.7.0

Adding ts-node to the tsconfig.json proved ineffective for me.ts-node添加到tsconfig.json对我来说是无效的。 I updated the tsconfig slightly ( see node14 base as a starting point):我稍微更新了tsconfig以 node14 base为起点):

{
  "compilerOptions": {
    "target": "ES2020",
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "skipLibCheck": true,
    "outDir": "build",
    "module": "es2020",
    "moduleResolution": "node"
  },
  "include": ["./*.ts"],
  "exclude": ["node_modules"]
}

I used the --loader ts-node/esm option:我使用了--loader ts-node/esm选项:

$ node --loader ts-node/esm ./index.ts

I also updated my smoke-test.ts module to look like this:我还更新了我的smoke-test.ts模块,如下所示:

function Log() {
  console.log('Smoke test running')
}

export const SmokeTest = { Log }

When importing with native esm modules, the extension must always be provided as .js for a .ts file:使用本机 esm 模块导入时,对于.ts文件,扩展名必须始终作为.js提供:

import { SmokeTest } from './smoke-test.js'
SmokeTest.Log()

I believe with ts-node v10.9.1 you can use the --esm flag我相信在ts-node v10.9.1 中你可以使用--esm标志

yarn ts-node --esm path/to/file.js

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

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