首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >获取错误:必须使用导入加载NodeJS脱机命令中的ES模块

获取错误:必须使用导入加载NodeJS脱机命令中的ES模块
EN

Stack Overflow用户
提问于 2022-08-14 14:16:24
回答 1查看 752关注 0票数 0

我创建了一个基于NodeJS / Typescript / Serverless框架的解决方案来创建AWS,并使用无服务器离线lib/plugin在VS代码中本地调试它。我的github回购是这里

执行命令时

代码语言:javascript
复制
sls offline -s dev

我得到了以下错误:

代码语言:javascript
复制
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /Users/hasan/apps/zaghop/v2/API/node_modules/serverless-offline/src/index.js
  require() of ES modules is not supported.
  require() of /Users/hasan/apps/zaghop/v2/API/node_modules/serverless-offline/src/index.js from /usr/local/lib/node_modules/serverless/lib/classes/PluginManager.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
  Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /Users/hasan/apps/zaghop/v2/API/node_modules/serverless-offline/package.json.
  
      at new NodeError (internal/errors.js:322:7)
      at Object.Module._extensions..js (internal/modules/cjs/loader.js:1102:13)
      at Module.load (internal/modules/cjs/loader.js:950:32)
    ....

我的tsconfig.json:

代码语言:javascript
复制
{
  "compilerOptions": {

    "target": "es6",                                /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
    "module": "commonjs",                           /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
    "lib": ["dom", "esnext"],                                   /* Specify library files to be included in the compilation. */
    "allowJs": true,                             /* Allow javascript files to be compiled. */
    "removeComments": true,                      /* Do not emit comments to output. */
    /* Strict Type-Checking Options */
    "strict": true,                                 /* Enable all strict type-checking options. */
    "noImplicitAny": false,                       /* Raise error on expressions and declarations with an implied 'any' type. */
    /* Module Resolution Options */
    "sourceMap": true,
    "moduleResolution": "node",                  /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
    "baseUrl": ".",                             /* Base directory to resolve non-absolute module names. */
    "paths": {                                   /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
        "@src/*": ["src/*"]
    },                                 
    "esModuleInterop": true,                        /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
    /* Experimental Options */
    "experimentalDecorators": true,              /* Enables experimental support for ES7 decorators. */
    "emitDecoratorMetadata": true,               /* Enables experimental support for emitting type metadata for decorators. */

    /* Advanced Options */
    "skipLibCheck": true,                           /* Skip type checking of declaration files. */
    "forceConsistentCasingInFileNames": true        /* Disallow inconsistently-cased references to the same file. */
  },
  "exclude": ["node_modules", "dist", "coverage"],
  "include": ["src"]
}

我现在的节点版本:

代码语言:javascript
复制
➜  node -v
v14.18.2

尝试将其更改为12,如下所示

代码语言:javascript
复制
➜  nvm use 12
Now using node v12.22.8 (npm v6.14.15)

那没什么用

有什么建议吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-08-15 11:45:44

我也面临着同样的问题。我通过安装v3 of serverless来解决这个问题。由于v9 of serverless-offline只支持Serverless的v3,所以在我将serverless更新为最新版本后,它就开始工作了。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73352359

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档