小编twa*_*sta的帖子

如何使用 nextjs 项目运行独立的 cli 脚本?

我有一个正在运行的 nextjs 项目。

我需要创建一个可以从 CLI 运行的脚本,该脚本使用我为下一个项目编写的一些 CRUD 库。

**/scripts/backup-assets.js **

import {getAllProjectsData} from '../lib/api/projects'

async function main() {
  const allProjectsData = await getAllProjectsData()
  console.info({allProjectsData})
}

main()
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

$ node scripts/backup-assets.js 
(node:9736) 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)
C:\Users\...\scripts\backup-assets.js:1
import {getAllProjectsData} from '../lib/api/projects'
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at Object.compileFunction (node:vm:352:18)
    at wrapSafe (node:internal/modules/cjs/loader:1033:15)
    at …
Run Code Online (Sandbox Code Playgroud)

node.js es6-modules next.js

11
推荐指数
2
解决办法
6132
查看次数

标签 统计

es6-modules ×1

next.js ×1

node.js ×1