javascript - NodeJS Typescrip.: Λήψη σφάλματος: Πρέπει να χρησιμοποιήσετε import για να φορτώσετε το ES Module στην εντολή sls offline
Έχω δημιουργήσει μια λύση βασισμένη σε NodeJS/Typescript/Serverless framework για τη δημιουργία AWS Lambdas και τη χρήση της βιβλιοθήκης/πρόσθετου serverless-offline για αποσφαλμάτωση τοπικά στο VS Code. Το αποθετήριο μου στο 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 μου:
➜ node -v
v14.18.2
Προσπάθησα να το αλλάξω σε 12 όπως παρακάτω
➜ nvm use 12
Now using node v12.22.8 (npm v6.14.15)
Αυτό δεν βοήθησε
Κάποιες προτάσεις;
Απάντηση #1
Αντιμετωπίζω επίσης το ίδιο πρόβλημα. Το έλυσα εγκαθιστώντας την έκδοση v3 του serverless. Δεδομένου ότι η έκδοση v9 του serverless-offline υποστηρίζει μόνο την v3 του Serverless, λειτούργησε αφότου ενημέρωσα το serverless στην τελευταία έκδοση.