title: AWS lambda function error - Unable to import module 'index': Error
body: <p>I'm trying to create a lambda in nodejs. Following the documentation, I have created a zip file with lambda function, node_modules and package.json. The structure of my lambda zip file is as follows:</p>
  
  <pre><code>my-lambda-function/
  |
  |---util/
  |     |
  |     |--util-1.js
  |     |--util-2.js
  |---api/
  |     |
  |     |--api-call-1.js
  |     |--api-call-2.js    
  |
  |---config
  |     |
  |     |--env/
  |     | |--env-file-1.js
  |     |
  |     |--config-file-1.js
  |     |--config-file-2.js
  |
  |---node_modules/
  |     |
  |     |--module-1/
  |     |--module-2/
  |---index.js
  |---package.json
  </code></pre>
  
  <p>For creating the this zip file I'm using a gulp task, </p>
  
  <pre><code>gulp.task('zip', ['test'], () =&gt; {
   const buildArtifact = ['index.js', 'package.json', 'util/**', 
   'config/**', 'api/**'];
   Object.keys(pjson.dependencies).forEach((dep) =&gt; {
    buildArtifact.push(`node_modules/${dep}/**/*`);
   });
   const zipFile = `${pjson.name}.zip`;
   return gulp.src(buildArtifact, { base: '.' })
     .pipe(zip(zipFile))
     .pipe(gulp.dest('build'));
  });
  </code></pre>
  
  <p>This lambda is running locally with lambda-local. But while testing this lambda is throwing error </p>
  
  <pre><code>Unable to import module 'index': Error
  at Function.Module._resolveFilename (module.js:547:15)
  at Function.Module._load (module.js:474:25)
  at Module.require (module.js:596:17)
  at require (internal/module.js:11:18)
  at Object.&lt;anonymous&gt; (/var/task/node_modules/ioredis/built/utils/lodash.js:2:19)
  at Module._compile (module.js:652:30)
  at Object.Module._extensions..js (module.js:663:10)
  at Module.load (module.js:565:32)
  </code></pre>
  
  <p>Current error is pointing to ioredis module (which is available in node_modues folder), but if I remove the usage of ioredis from the code (by removing the imports for ioredis) then the error changes to another node module. </p>
  
  <p>It seems my lambda function is unable to load/find the node_modules. 
  Is it due to lambda is failing to install npm?</p>
  
  <p>Any pointers to solve this would be helpful as we are struggling with this for a few days</p>
  
  <p>Thanks</p>
  
  <h2>More details</h2>
  
  <ol>
  <li>We can reproduce the error by deleting the node_modules folder and then running the lambda locally</li>
  <li>If I remove all my dependencies, a trivial logging statement will work.</li>
  <li>The lambda is using node 8.10 in aws</li>
  </ol>
  
  <h2>Answer (@Kannaiyan)</h2>
  
  <p>Changing the zip task to:</p>
  
  <pre><code>gulp.task('npm-install', () =&gt; gulp.src('./package.json')
    .pipe(gulp.dest('build/'))
    .pipe(install({ production: true })));
  </code></pre>
  
  <p>worked for me.</p>