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

安装Artillery包后NodeJS出现MODULE_NOT_FOUND错误求助

Artillery模块导入报错:Cannot find module '.../artillery/lib'

问题详情

运行以下Node.js脚本:

const fs = require('fs');
const { Test } = require('artillery');

const options = {
  'key': fs.readFileSync('./key.pem'),
  cert: fs.readFileSync('./certificates.pem'),
  ca: fs.readFileSync('./ca.pem')
};

const test = new Test(config, options);

test.run((err, results) => {
  if (err) {
      console.error(err);
  } else {
      console.log(results);
  }
});

执行命令 node myscript.js 后收到错误提示:

Cannot find module 'CL\path\to\node_modules\artillery\lib'. Please verify that the package.json has a valid "main" entry

已尝试的操作:

  • 删除node_modules和package-lock.json后执行npm install重装依赖
  • 清除npm缓存
  • 卸载并重新安装Artillery包
  • 确认npm、Node.js和Artillery均为最新版本

项目的package.json内容:

{
  "name": "helloworld",
  "version": "0.0.0",
  "private": true,
  "main": "./bin/www",
  "scripts": {
    "start": "node ./bin/www"
  },
  "dependencies": {
    "artillery": "^2.0.0-27",
    "artillery-plugin-http-ssl-auth": "^0.0.1",
    "axios": "^1.2.2",
    "cookie-parser": "~1.4.4",
    "date-fns": "^2.29.3",
    "debug": "^4.3.4",
    "express": "^4.18.2",
    "http-errors": "~1.6.3",
    "https": "^1.0.0",
    "jshint": "^2.13.6",
    "morgan": "^1.3.2",
    "pug": "^2.0.4"
  },
  "devDependencies": {
    "@types/artillery": "^1.7.0"
  }
}

解决方法

1. 检查Artillery包的实际结构

打开node_modules/artillery目录,确认是否存在lib文件夹,以及该文件夹下是否有主入口文件(如index.js)。若lib目录缺失,说明包安装不完整,可尝试切换网络环境后重新执行npm install。

2. 切换到Artillery稳定版本

当前使用的是预发布版本^2.0.0-27,这类版本可能存在兼容性问题。尝试降级到稳定版:

npm uninstall artillery
npm install artillery@1.7.9

3. 修正导入路径

查看node_modules/artillery/package.json中的main字段,确认正确的入口路径,调整导入语句。例如如果主入口是lib/index.js,可修改为:

const { Test } = require('artillery/lib');

4. 对齐类型定义版本

devDependencies中的@types/artillery是^1.7.0,与Artillery v2预发布版不匹配,可先卸载类型定义:

npm uninstall @types/artillery

5. 检查项目路径

错误提示中的路径存在解析异常,确认项目路径是否包含空格、中文或特殊字符,将项目移到路径简洁的目录下(如C:\projects\artillery-test)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 06:30:47