相关疑难解决方法(0)

NextJS:在服务器上运行 Typescript 脚本

我有一个 NextJS/Typescript 项目,我想在其中添加一个 CLI 脚本,该脚本应该处理服务器上的一些文件。

不幸的是我无法运行该脚本。

示例脚本src/cli.ts

console.log("Hello world");
// Process files
Run Code Online (Sandbox Code Playgroud)

我尝试使用以下命令运行脚本:

ts-node src/cli.ts
Run Code Online (Sandbox Code Playgroud)

但我收到此错误消息:

src/cli.ts:1:1 - error TS1208: 'cli.ts' cannot be compiled under '--isolatedModules' because it is considered a global script file. Add an import, export, or an empty 'export {}' statement to make it a module.
Run Code Online (Sandbox Code Playgroud)

当我添加空的“export {}”语句时,我收到以下错误消息:

(node:15923) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
Run Code Online (Sandbox Code Playgroud)

据我所知,现在不可能将 NextJS 与 ES 模块一起使用。

还有另一种方法可以在 NextJS 项目中运行脚本吗?也许我需要更改 webpack …

typescript next.js ts-node

7
推荐指数
3
解决办法
4757
查看次数

标签 统计

next.js ×1

ts-node ×1

typescript ×1