I am trying to figure out the recommended way to include libraries outside of the project folder. From what I could find, it seems that there used to be a method called ‘magic methods’ that was depreciated in v1. With version 1.x people suggest using:
npm install <path/to/lib>
or
npm link
and the related commands for linking. In the first case, npm appears to correctly install the library, which is confirmed by looking inside the package.json file. For example, mine looks like:
{
"name": "dynamodb-stream-handlers",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"aws-sdk": "^2.177.0",
"babel-core": "^6.26.0",
"babel-loader": "^7.1.2",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-preset-env": "^1.6.1",
"babel-preset-stage-3": "^6.24.1",
"serverless-plugin-aws-alerts": "^1.2.4",
"serverless-webpack": "^4.2.0",
"webpack": "^3.10.0",
"webpack-node-externals": "^1.6.0"
},
"dependencies": {
"babel-runtime": "^6.26.0",
"libs": "file:../libs"
}
}
As you can see, libs is indeed under dependencies. However, when, in my lambda function handler use the line:
const libs = require('libs');
I get an error:
{ Error: Cannot find module 'libs'
at Function.Module._resolveFilename (module.js:538:15)
at Function.Module._load (module.js:468:25)
at Module.require (module.js:587:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/Users/user/Documents/GitHub/serverless-cloud-functions/dynamodb-stream-handlers/.webpack/service/properties-stream-handler.js:423:18)
at __webpack_require__ (/Users/user/Documents/GitHub/serverless-cloud-functions/dynamodb-stream-handlers/.webpack/service/properties-stream-handler.js:20:30)
at Object.<anonymous> (/Users/user/Documents/GitHub/serverless-cloud-functions/dynamodb-stream-handlers/.webpack/service/properties-stream-handler.js:379:12)
at __webpack_require__ (/Users/user/Documents/GitHub/serverless-cloud-functions/dynamodb-stream-handlers/.webpack/service/properties-stream-handler.js:20:30)
at Object.defineProperty.value (/Users/user/Documents/GitHub/serverless-cloud-functions/dynamodb-stream-handlers/.webpack/service/properties-stream-handler.js:63:18)
at Object.<anonymous> (/Users/user/Documents/GitHub/serverless-cloud-functions/dynamodb-stream-handlers/.webpack/service/properties-stream-handler.js:66:10)
at Module._compile (module.js:643:30)
at Object.Module._extensions..js (module.js:654:10)
at Module.load (module.js:556:32)
at tryModuleLoad (module.js:499:12)
at Function.Module._load (module.js:491:3)
at Module.require (module.js:587:17)
at require (internal/module.js:11:18)
at AwsInvokeLocal.invokeLocalNodeJs (/Users/user/.nvm/versions/node/v8.9.4/lib/node_modules/serverless/lib/plugins/aws/invokeLocal/index.js:258:33)
at AwsInvokeLocal.invokeLocal (/Users/user/.nvm/versions/node/v8.9.4/lib/node_modules/serverless/lib/plugins/aws/invokeLocal/index.js:125:19)
at AwsInvokeLocal.tryCatcher (/Users/user/.nvm/versions/node/v8.9.4/lib/node_modules/serverless/node_modules/bluebird/js/release/util.js:16:23)
at Promise._settlePromiseFromHandler (/Users/user/.nvm/versions/node/v8.9.4/lib/node_modules/serverless/node_modules/bluebird/js/release/promise.js:512:31)
at Promise._settlePromise (/Users/user/.nvm/versions/node/v8.9.4/lib/node_modules/serverless/node_modules/bluebird/js/release/promise.js:569:18)
at Promise._settlePromise0 (/Users/user/.nvm/versions/node/v8.9.4/lib/node_modules/serverless/node_modules/bluebird/js/release/promise.js:614:10)
at Promise._settlePromises (/Users/user/.nvm/versions/node/v8.9.4/lib/node_modules/serverless/node_modules/bluebird/js/release/promise.js:693:18)
at Async._drainQueue (/Users/user/.nvm/versions/node/v8.9.4/lib/node_modules/serverless/node_modules/bluebird/js/release/async.js:133:16)
at Async._drainQueues (/Users/user/.nvm/versions/node/v8.9.4/lib/node_modules/serverless/node_modules/bluebird/js/release/async.js:143:10)
at Immediate.Async.drainQueues (/Users/user/.nvm/versions/node/v8.9.4/lib/node_modules/serverless/node_modules/bluebird/js/release/async.js:17:14)
at runCallback (timers.js:789:20)
at tryOnImmediate (timers.js:751:5)
at processImmediate [as _immediateCallback] (timers.js:722:5) code: 'MODULE_NOT_FOUND' }
Similarly, using the link method, I get the same results, even though I can see the symlink when using the command:
ls -al $(npm root -g)
My hunch is that there is some incompatibility with babel and/or webpack, but I’m not sure. Is there another option besides the two I tried?