#Argon2 import not working

32 messages · Page 1 of 1 (latest)

weak heart
#
ERROR in ./node_modules/argon2/lib/binding/napi-v3/argon2.node 1:0
Module parse failed: Unexpected character '' (1:0)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
(Source code omitted for this binary file)
dire bobcat
#

Is this while running, while installing argon, while doing something else?

weak heart
#

I have a monorepo. It's when building with yarn build

dire bobcat
#

Ah, this would most likely be because you're trying to bundle everyhting (node_modules included) into a single file. We usually don't recommend that for Node servers

#

Do you have a customized webpack.config.js file?

weak heart
#
module.exports = (options, webpack) => {
  const lazyImports = [
    '@nestjs/microservices/microservices-module',
    '@nestjs/websockets/socket-module',
    '@nestjs/platform-express'
  ]

  return {
    ...options,
    externals: [],
    output: {
      ...options.output,
      libraryTarget: 'commonjs2'
    },
    plugins: [
      ...options.plugins,
      new webpack.IgnorePlugin({
        checkResource(resource) {
          if (lazyImports.includes(resource)) {
            try {
              require.resolve(resource)
            } catch (err) {
              return true
            }
          }
          return false
        }
      })
    ]
  }
}
dire bobcat
#

Ah, yeah, externals: []. I would not recommend that

weak heart
#

My tsconfig.build.json is ignoring it

{
  "extends": "./tsconfig.json",
  "exclude": ["node_modules", "test", "dist", "**/*spec.ts"]
}
dire bobcat
#

That's just the Typescript compiler ignoring the file. Webpack will still try to bundle it

weak heart
#

Ah

#

Webpack isn't really my strong point, so do you recommend anything in particular?

#

I just took this config out the docs for a quick fix

dire bobcat
#

Remove the externals: [] and I think everything should work because Nest's default adds in the fact that node_modules should be external to the bundle (meaning you still need to install them)

weak heart
#

Hmm, OK so now my serverless handler is not being recognised as a Function

#

This happened earlier with a fix that I tried. What I did was add this in my config:

exclude:path.resolve(__dirname, "node_modules")

And it gave me the new error, which this solution now does too

dire bobcat
#

No clue about serverless to be honest. Never dabbled wit hthat

weak heart
#

But it's related to the change I just made. If I remove argon2 imports and reverse the changes, it runs fine

#

I'm thinking it might even be easier to uninstall argon2 and try bycrypt, this is causing too much troube

dire bobcat
#

If you go with bcrypt use bcryptjs. Otherwise you'll get the same issue

#

bcrypt and argon both normally use c++ bindings for the extra speed. bcryptjs is a pure js implementation of bcrypt. It's about 30% slower, but doesn't require c++ bindings

weak heart
#

That'll be fine then, I will try a bit to solve this though

#

I'm using the aws-lambda package. You reckon I need to add that in the webpack config somewhere? maybe it's being ignored now

#

maybe in the lazyimports?

#

I import the handler as such import { Callback, Context, Handler } from 'aws-lambda'

dire bobcat
#

What's the specific error you're getting?

weak heart
#
✖ offline: handler 'handler' in /home/Documents/Code/app/dist/apps/api/main is not a function
✖ Error: offline: handler 'handler' in /home/Documents/Code/app/dist/apps/api/main is not a function

When I make a request to the endpoint.

I posted in the serverless group too as someone might know but I suspect it's something to do with webpack

dire bobcat
#

That would seem like handler is being viewed as undefined. Can you show how you define it?

weak heart
#

Sure, this is my main file

import { configure as serverlessExpress } from '@vendia/serverless-express'
import { Callback, Context, Handler } from 'aws-lambda'
import { NestFactory } from '@nestjs/core'
import compression from 'compression'
import { IndicatorsApiModule } from './indicators-api.module'

let server: Handler

async function bootstrap(): Promise<Handler> {
  const app = await NestFactory.create(IndicatorsApiModule)
  await app.init()
  app.enableCors()
  app.use(compression())

  const expressApp = app.getHttpAdapter().getInstance()
  return serverlessExpress({ app: expressApp })
}

export const handler: Handler = async (event: any, context: Context, callback: Callback) => {
  server = server ?? (await bootstrap())
  return server(event, context, callback)
}
#

I can see the file main in dist/api/main. The transpiled code has the Handler inside it. I think it's having trouble now importing from the aws-lambda lib

#

very strange for me to understand how after removing excludes: []

dire bobcat
#

Handler is just a type. It shouldn't effect your runtime. This seems like the exported const named handler is an issue

#

Why, again, no clue. Serverless has never made sense to me