【发布时间】:2022-01-05 19:31:27
【问题描述】:
在 Node 中尝试最简单的导入,但我无法让它工作.... (根据网络上的建议添加了“名称”:“Foo”。)
package.json
{
"type": "module",
"name": "Foo",
"version": "1.0.0",
"description": "",
"main": "index.js",
"author": "",
"license": "ISC",
}
index.ts
import foo from './foo';
foo();
foo.ts
export default function foo() {
console.log("FOO");
}
结果
import foo from './foo';
^^^^^^
SyntaxError: Cannot use import statement outside a module
at wrapSafe (internal/modules/cjs/loader.js:1072:16)
at Module._compile (internal/modules/cjs/loader.js:1122:27)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)
at Module.load (internal/modules/cjs/loader.js:1002:32)
at Function.Module._load (internal/modules/cjs/loader.js:901:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)
at internal/main/run_main_module.js:18:47
【问题讨论】:
-
您是使用
ts-node(npx ts-node ./index.ts) 还是node运行脚本? -
你是如何让你的 TypeScript 在 Node 上运行的?你在转译吗?你在用 ts-node 吗?
-
@user3210641 $ 节点索引.ts
-
@Quentin $ node index.ts
-
@Oded — 这就是问题所在。 Node.js 不支持 TypeScript。
标签: javascript node.js typescript