Node.js - 单个可执行应用程序:语法错误:无法在模块外部使用 import 语句

问题描述 投票:0回答:1

按照此文档将node.js应用程序(在Windows 10上)作为单个可执行应用程序运行时遇到问题。

出现此错误:

.\main.exe
(node:12216) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `main --trace-warnings ...` to show where the warning was created)
main.js:28
import * as apiKeysHandler from "./helpers/apiKeysHandler.js";
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at internalCompileFunction (node:internal/vm:77:18)
    at wrapSafe (node:internal/modules/cjs/loader:1288:20)
    at embedderRunCjs (node:internal/util/embedding:19:27)
    at node:internal/main/embedding:18:8

Node.js v20.11.0

sea-config.json
的内容是:

{
    "main": "main.js",
    "output": "sea-prep.blob"
}

我知道我需要修改我的

package.json
文件来加载 ES 模块(这也是我上面列出的输出中的警告)。因此我的
package.json
看起来像:

{
  "main": "main.js",
  "type": "module",
  "dependencies": {
    "sync-request": "^6.1.0"
  }
}

我已经尝试将

"type": "module"
添加到我的
sea-config.json
中,但这没有改变任何东西。

当我在命令行上使用

node.exe
运行
main.js
时,没有错误,我的应用程序运行良好。当我在同一目录中运行
main.exe
时,会出现上面列出的错误。

有人可以告诉我我的错误吗?谢谢!

javascript node.js windows
1个回答
0
投票
@sychordCoder 感谢您的回复。这是我的文件夹结构:

    main.js
  • 帮手
    • apiKeysHandler.js
  • .gitignore
  • package.json
  • package-lock.json`
© www.soinside.com 2019 - 2024. All rights reserved.