TypeScript, Serverless and Webpack complications

20
November 26, 2020, at 05:10 AM

I am trying to use typescript with serverless applications and I ran in some issues like the serverless-typescript package had its last update a year ago. I also tryed to use it with serverless webback but i am running in some issues regarding the use of ORMS because webpack cannot load the modals properly (it can when i import the files but not a runtime). Does someone have any advice of how to devolop serverless applications with typescript or should I keep using just javascript?

Answer 1

Basically it doesn't matter either it is serverless application or not. What matters is your webpack config. Try using serverless-webpack plugin with proper webpack config for typescript compilation

serverless.yml webpack part example:

plugins:
  - serverless-webpack
custom:
  webpack:
    webpackConfig: ./webpack.config.js

webpack.config.js example:

const path = require('path')
const slsw = require('serverless-webpack')
module.exports = {
  mode: 'development',
  devtool: 'source-map',
  entry: slsw.lib.entries,
  target: 'node',
  resolve: {
    extensions: ['.mjs', '.ts', '.js', '.json', '.tsx']
  },
  output: {
    libraryTarget: 'commonjs2',
    path: path.join(__dirname, '.webpack'),
    filename: '[name].js'
  },
  module: {
    rules: [
      {
        oneOf: [
          {
            test: /\.(ts|js)$/,
            exclude: [/node_modules/],
            loader: 'ts-loader',
            options: {
              transpileOnly: true
            }
          }
        ]
      }
    ]
  }
}

You can adapt it to your needs, I've just shared a little piece of my config

READ ALSO
How to handle errors in async Express?

How to handle errors in async Express?

Context: I have a rather large project which is a social media back endWe use MondoDB (w/ mongoose) and Express in JS

29
How can i redirect user to registration page when he failed to enter correct email (ID) in AMP form?

How can i redirect user to registration page when he failed to enter correct email (ID) in AMP form?

Please kindly advise if there is walk-around to redirect to new page when amp-form validation returns error? ie

43
NodeJS : The media could not be loaded, either because the server or network failed or because the format is not supported

NodeJS : The media could not be loaded, either because the server or network failed or because the format is not supported

I have a NodeJS server and I'd like to serve a local video, but I get an error when getting the link to the html page that says: The media could not be loaded, either because the server or network failed or because the format is not supported

54
Cannot find a module after run npm install (Docker)

Cannot find a module after run npm install (Docker)

I'm trying to dockerize a node-app, but for some reason when I start the app using:

68