Skip to content

ts-node inside AWS Lambda cannot resolve dependencies in /opt/nodejs/node_modules #1430

Description

@SleepyDeb

Hi,

Using ts-node I'm trying to use Typescript directly inside lambda function on AWS, to do this I'm using this sample code:

require('ts-node/register');
console.log('Register!');

const { main } = require('./src/test.ts');

exports.handler = async function handler() {
  console.log('Starting...');
  await main();
  console.log('Done!');
};

The problem is that in my lambda the node_modules directory is placed in a layer /opt/nodejs/node_modules

So what happens is that ts-node doesn't find libraries on the imported sources giving errors such:

src/test.ts(2,21): error TS2307: Cannot find module '@aws-sdk/client-s3' or its corresponding type declarations.

I'm trying to override the configuration of ts-node using this code, but still no luck.

var tsNode = require('ts-node');

var service = tsNode.register({
    lazy: true,
    files: false,
    compilerOptions: {
        "target": "ES2019",
        "module": "commonjs",
        "lib": [
            "DOM",
            "ES6",
            "ES2019",
            "ES2020"
        ],
        "declaration": true,
        "strict": false,
        "noImplicitAny": false,
        "strictNullChecks": true,
        "noImplicitThis": true,
        "alwaysStrict": true,
        "noUnusedLocals": false,
        "noUnusedParameters": false,
        "noImplicitReturns": true,
        "noFallthroughCasesInSwitch": false,        
        "experimentalDecorators": true,
        "emitDecoratorMetadata": true,
        "inlineSourceMap": true,
        "inlineSources": true,
        "strictPropertyInitialization": true,
        "baseUrl": "/opt/nodejs/",
        "types": [ 
            "node",
            "aws-lambda" 
        ],
        "typeRoots": [
            "node_modules",
            "node_modules/@types",
        ]
    }
});

Thanks you in advance,
Regards

Activity

  1. locked and limited conversation to collaborators on Aug 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions