You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在VSCode中配置纯JavaScript多文件启动项目

多JS文件调用解决方案

操作前建议新建空白测试文件夹验证,避免旧配置干扰,按你的运行场景选择对应方案即可:

场景1:用Node.js直接运行JS(当前无HTML/CSS的场景)

方案A:使用Node默认的CommonJS规范(零额外配置)

  1. 修改folder-02/test-02.js,末尾添加导出语句:
//test-02.js
function Display(param = 0) {
    console.log("This is the program called with parameter: ", param);
    return "Back from Display";
};
// 新增导出代码
module.exports = Display;
  1. 修改根目录的test-01.js,开头导入函数:
//test-01.js
// 新增导入代码
const Display = require('./folder-02/test-02.js');

let returnStr = "";
console.log("This is the calling program");
returnStr = Display(10);
  1. 终端运行命令即可正常执行:node test-01.js

方案B:使用ES Module规范(支持import/export语法)

  1. 根目录新建package.json,仅需添加一行配置即可,其余字段不用加:
{
  "type": "module"
}
  1. 修改folder-02/test-02.js,末尾添加导出语句:
//test-02.js
function Display(param = 0) {
    console.log("This is the program called with parameter: ", param);
    return "Back from Display";
};
// 新增导出代码
export { Display };
  1. 修改根目录的test-01.js,开头导入函数:
//test-01.js
// 新增导入代码,必须写全.js后缀
import { Display } from './folder-02/test-02.js';

let returnStr = "";
console.log("This is the calling program");
returnStr = Display(10);
  1. 终端运行命令即可正常执行:node test-01.js

场景2:后续接入浏览器运行的适配方案

直接用上述ES Module规范的代码即可,引入test-01.js的script标签需要添加type="module"属性:

<script src="test-01.js" type="module"></script>

环境异常修复提示:把之前自行修改的jsconfig.json、launch.json、settings.json等非必要配置全部删除,用空白文件夹按上述步骤测试即可正常运行,无需额外修改编辑器配置。

内容的提问来源于stack exchange,提问作者Thomas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.25 21:36:04