为何我不能用ts-node运行import的ts文件?

我创建了一个ts文件如下:

image.png

当我运行时候报错:

$ npx ts-node test/unit/ts-morph-test.ts 
(node:5189) 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)
/Users/markleo/Desktop/Test/js/typescript/ts-test01/my-app/test/unit/ts-morph-test.ts:1
import { Project } from "ts-morph";
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at Object.compileFunction (node:vm:360:18)
    at wrapSafe (node:internal/modules/cjs/loader:1084:15)
    at Module._compile (node:internal/modules/cjs/loader:1119:27)
    at Module.m._compile (/Users/markleo/Desktop/Test/js/typescript/ts-test01/node_modules/ts-node/src/index.ts:1618:23)
    at Module._extensions..js (node:internal/modules/cjs/loader:1209:10)
    at Object.require.extensions.<computed> [as .ts] (/Users/markleo/Desktop/Test/js/typescript/ts-test01/node_modules/ts-node/src/index.ts:1621:12)
    at Module.load (node:internal/modules/cjs/loader:1033:32)
    at Function.Module._load (node:internal/modules/cjs/loader:868:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at phase4 (/Users/markleo/Desktop/Test/js/typescript/ts-test01/node_modules/ts-node/src/bin.ts:649:14)
mba:my-app markleo$ npx ts-node test/unit/ts-morph-test.ts 
(node:5278) 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)
/Users/markleo/Desktop/Test/js/typescript/ts-test01/my-app/test/unit/ts-morph-test.ts:1
import { Project } from "../../node_modules/ts-morph";
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at Object.compileFunction (node:vm:360:18)
    at wrapSafe (node:internal/modules/cjs/loader:1084:15)
    at Module._compile (node:internal/modules/cjs/loader:1119:27)
    at Module.m._compile (/Users/markleo/Desktop/Test/js/typescript/ts-test01/node_modules/ts-node/src/index.ts:1618:23)
    at Module._extensions..js (node:internal/modules/cjs/loader:1209:10)
    at Object.require.extensions.<computed> [as .ts] (/Users/markleo/Desktop/Test/js/typescript/ts-test01/node_modules/ts-node/src/index.ts:1621:12)
    at Module.load (node:internal/modules/cjs/loader:1033:32)
    at Function.Module._load (node:internal/modules/cjs/loader:868:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at phase4 (/Users/markleo/Desktop/Test/js/typescript/ts-test01/node_modules/ts-node/src/bin.ts:649:14)

如果我用require就可以引入。
执行成功。

let tsmorph = require('ts-morph') 


// 创建一个TypeScript项目对象
const project = new tsmorph.Project();

// 从文件系统加载tsconfig.json文件,并将其中的所有源文件添加到项目中
project.addSourceFilesAtPaths("./tsconfig.json");

// 获取项目中的所有源文件
const sourceFiles = project.getSourceFiles();

console.log(sourceFiles)

是否这个意思意味的是,ts-morph 这个库没有导出esm模块,只导出了cjs模块?

image.png

阅读 2.5k
1 个回答

错误信息不是告诉你原因以及怎么修改了么……

To load an ES module, set "type": "module" in the package.json or use the .mjs extension.

P.S. 平时你在 React/Vue 项目里 ESM/CommonJS 随意用是因为 Webpack 等构建工具做了处理,Node.js 从来默认都只支持 CommonJS。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
logo
Microsoft
子站问答
访问
宣传栏