В настоящее время я конвертирую бессерверный API-интерфейс лямбда-микросервисов AWS в машинописный текст. Я надеюсь, что смогу поддерживать существующие файлы js, добавляя больше файлов ts по мере продвижения. У меня возникли проблемы с настройкой веб-пакета и машинописного текста, и я получаю эту ошибку во время выполнения при вызове одной из моих лямбда-выражений.
× Unhandled exception in handler 'getInstitutionInfo'.
× Runtime.UserCodeSyntaxError: SyntaxError: Cannot use import statement outside a module
at _loadUserApp (C:\git\my-project\node_modules\serverless-offline\src\lambda\handler-runner\in-process-runner\aws-lambda-ric\UserFunction.js:307:15)
at async module.exports.load (C:\git\my-project\node_modules\serverless-offline\src\lambda\handler-runner\in-process-runner\aws-lambda-ric\UserFunction.js:341:21)
at async InProcessRunner.run (file:///C:/git/my-project/node_modules/serverless-offline/src/lambda/handler-runner/in-process-runner/InProcessRunner.js:41:21)
at async MessagePort.<anonymous> (file:///C:/git/my-project/node_modules/serverless-offline/src/lambda/handler-runner/worker-thread-runner/workerThreadHelper.js:24:14)
× SyntaxError: Cannot use import statement outside a module
Кажется, жалуется на один из пакетов, которые я использую, а не на мой код. Это мой tsconfig.json
{
"$schema": "https://json.schemastore.org/tsconfig",
"compilerOptions": {
"lib": [
"es2020"
],
"module": "commonjs",
"target": "es2020",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"sourceMap": true,
"allowJs": true,
"outDir": ".erb/dll"
}
}
и моя конфигурация веб-пакета
const path = require('path');
const slsw = require('serverless-webpack');
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
const nodeExternals = require('webpack-node-externals');
module.exports = {
// `mode` will be set to `production` and comes with included optimizations
// when building to be run on AWS or similar.
// https://webpack.js.org/configuration/mode/
mode: slsw.lib.webpack.isLocal ? 'development' : 'production',
// to determine what source maps to use per dev or prod
// https://webpack.js.org/configuration/devtool/
devtool: slsw.lib.webpack.isLocal ? 'source-map' : 'cheap-source-map',
// the provided argument will be an object referencing functions as defined
// in your `serverless.yml` .
// https://webpack.js.org/concepts/entry-points/
entry: slsw.lib.entries,
target: 'node',
resolve: {
// What file extensions we want Webpack to care about, and in what order
// https://webpack.js.org/configuration/resolve/#resolveextensions
extensions: ['.cjs', '.mjs', '.js', '.ts'],
// `yarn add -D tsconfig-paths-webpack-plugin` if you need path aliases
// plugins: [new TsconfigPathsPlugin()],
},
// Where the bundled files will be output. Not strictly necessary with
// Serverless Webpack.
// https://webpack.js.org/configuration/output/
output: {
libraryTarget: 'commonjs2',
path: path.join(__dirname, '.webpack'),
filename: '[name].js',
},
// Anything that will be available to the bundled code in the runtime
// environment and does not need to be included in any of the bundles.
//
// In AWS Lambda, the `aws-sdk` is available and we almost certainly want to
// exclude it from our bundle(s). Similarly, because it's a Node lambda,
// Node's native modules will also be available.
externals: ['aws-sdk', nodeExternals()],
module: {
// Instruct Webpack to use the `ts-loader` for any TypeScript files, else it
// won't know what to do with them.
rules: [
{
test: /\.[jt]s?$/,
loader: 'ts-loader',
exclude: [
[
/node_modules/,
path.resolve(__dirname, '.webpack'),
path.resolve(__dirname, '.serverless'),
],
],
// And here we have options for ts-loader
// https://www.npmjs.com/package/ts-loader#options
options: {
// Disable type checking, this will lead to improved build times
transpileOnly: true,
// Enable file caching, can be quite useful when running offline
experimentalFileCaching: true,
},
},
],
},
// We still want type checking, just without the burden on build performance,
// so we use a plugin to take care of it on another thread.
plugins: [new ForkTsCheckerWebpackPlugin()],
};
@morganney Может быть глупый вопрос, но как мне это узнать? Я предполагаю, что это то, во что скомпилирован код, то есть commonjs. В package.json не указан «тип», а версия узла, на которой он развернут, равна 18.
Node 18 поддерживает esm. Я бы предложил перенести ваше приложение на цель сборки esm. В противном случае вы добавляете дополнительные инструменты сборки для поддержки старой модульной системы. Вы также можете увидеть, есть ли у этой зависимости версия, опубликованная как cjs, и использовать ее вместо этого.





Стек вызовов был отвлекающим маневром, поскольку он не показывал стек вызовов, где на самом деле произошла ошибка. Проблема была с пакетом npm, который мы создали с помощью импорта esm, но он был импортирован в проект, использующий commonjs. Исправление состояло в том, чтобы пересобрать пакет с помощью babel, чтобы заменить его на commonjs.
Ваша лямбда работает как esm или cjs? Ваша конфигурация TS предполагает, что вы хотите cjs, и ошибка подразумевает, что в вашем проекте не применяется модуль типа. Можете ли вы переключиться на esm, т. е. использовать среду выполнения узла, которая его поддерживает? В противном случае вам понадобится что-то вроде babel для переноса этой зависимости для использования cjs.