Nodejs引入模块运行时报错

在做自动化部署的时候,引入包管理,运行脚本的时候报错:

const path = require('path')
const fs = require('fs')
const Client = require('ssh2-sftp-client')

报错信息如下:

PS D:\CODING\Projects\vue-admin-template> node deploy/index.mjs
file:///D:/CODING/Projects/vue-admin-template/deploy/index.mjs:1
const path = require('path')
             ^

ReferenceError: require is not defined in ES module scope, you can use import instead
    at file:///D:/CODING/Projects/vue-admin-template/deploy/index.mjs:1:14
    at ModuleJob.run (internal/modules/esm/module_job.js:183:25)
    at async Loader.import (internal/modules/esm/loader.js:178:24)
    at async Object.loadESM (internal/process/esm_loader.js:68:5)
    at async handleMainPromise (internal/modules/run_main.js:59:12)

根据提示首先将require改成import,运行报错:

PS D:\CODING\Projects\vue-admin-template> node deploy/index.js 
(node:16164) 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)
D:\CODING\Projects\vue-admin-template\deploy\index.js:1
import { join } from 'path'
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at wrapSafe (internal/modules/cjs/loader.js:1001:16)
    at Module._compile (internal/modules/cjs/loader.js:1049:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
    at Module.load (internal/modules/cjs/loader.js:950:32)
    at Function.Module._load (internal/modules/cjs/loader.js:790:12)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:75:12)
    at internal/main/run_main_module.js:17:47

根据报错提示,尝试两种解决方案:
1.在package.json中使用"type": "module":不可行,这将覆盖整个项目,导致项目启动不了。
2.将文件更改为index.mjs,表示引入module模块。最终解决问题。

相关推荐

  1. Nodejs引入模块运行时报

    2024-03-16 00:20:06       45 阅读
  2. 解决nodejs内存泄漏问题,项目无法运行

    2024-03-16 00:20:06       55 阅读
  3. 基于ts的node项目引入归纳

    2024-03-16 00:20:06       62 阅读
  4. Vue 打包或运行时报Error: error:0308010C

    2024-03-16 00:20:06       205 阅读
  5. node.js 模版引擎

    2024-03-16 00:20:06       35 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-16 00:20:06       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-16 00:20:06       100 阅读
  3. 在Django里面运行非项目文件

    2024-03-16 00:20:06       82 阅读
  4. Python语言-面向对象

    2024-03-16 00:20:06       91 阅读

热门阅读

  1. 安卓自定义ScrollView

    2024-03-16 00:20:06       34 阅读
  2. 技术问题记录

    2024-03-16 00:20:06       47 阅读
  3. Spring Cloud面试系列-01

    2024-03-16 00:20:06       49 阅读
  4. 【gpt实践】实用咒语分享

    2024-03-16 00:20:06       44 阅读
  5. uniapp内实现链接跳转到浏览器网页上

    2024-03-16 00:20:06       42 阅读
  6. jdk8与jdk17的区别。springboot2.x与springboot3.x的区别

    2024-03-16 00:20:06       45 阅读
  7. 4.Python从入门到精通—Python 基础语法详细讲解-下

    2024-03-16 00:20:06       39 阅读
  8. Reactor,Proactor,Actor网络模型

    2024-03-16 00:20:06       31 阅读
  9. 蓝桥杯第1167题——荷马史诗

    2024-03-16 00:20:06       45 阅读
  10. 用 C 语言模拟 Rust 的 Box 类型

    2024-03-16 00:20:06       38 阅读
  11. Flink读取iceberg表

    2024-03-16 00:20:06       41 阅读