首页 > 解决方案 > VSCode 内部人员节点调试器启动 src 文件而不是构建

问题描述

因此,我有一个非常整洁的项目,并进行了一些非常简洁的调试,该调试一直有效,但经过太长时间的中断后,现在我似乎无法使其正常工作。就像我在调试选项卡中注意到的一样

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)
/home/inaba/Programming/Lewd/packages/main-site/src/index.js:1
import express from "express";

这是我的launch.json,它一直有效

{
  "type": "node",
  "request": "launch",
  "name": "Launch Main App",
  "preLaunchTask": "npm: build",
  "program": "src/index.js",
  "sourceMaps": true,
  "outFiles": [
    "dist/**/*.js"
  ],
  "envFile": "${workspaceFolder}/../../.env",
  "runtimeArgs": [
    "--require=dotenv/config"
  ]
}

那么到底是什么?据我所知,我做的事情对吗?

标签: debuggingvisual-studio-code

解决方案


尝试 require() 而不是 import 语句

在这里引用节点文档:

import 语句只允许在 ES 模块中使用。有关 CommonJS 中的类似功能,请参阅 import()。

https://nodejs.org/api/esm.html#esm_import_statements

这里有一些详细的上下文:Using Node.js require vs. ES6 import/export


推荐阅读