我创建了一个基于NodeJS / Typescript / Serverless框架的解决方案来创建AWS,并使用无服务器离线lib/plugin在VS代码中本地调试它。我的github回购是这里
执行命令时
sls offline -s dev我得到了以下错误:
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:
{
"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"]
}我现在的节点版本:
➜ node -v
v14.18.2尝试将其更改为12,如下所示
➜ nvm use 12
Now using node v12.22.8 (npm v6.14.15)那没什么用
有什么建议吗?
发布于 2022-08-15 11:45:44
我也面临着同样的问题。我通过安装v3 of serverless来解决这个问题。由于v9 of serverless-offline只支持Serverless的v3,所以在我将serverless更新为最新版本后,它就开始工作了。
https://stackoverflow.com/questions/73352359
复制相似问题