serverless-runtime-babel icon indicating copy to clipboard operation
serverless-runtime-babel copied to clipboard

How to handle require(<local dir>)

Open thenikso opened this issue 9 years ago • 3 comments

Hello (again),

So some node_modules have requires like so:

var something = require(__dirname + '/something.js');

And, when deployed, will throw an error like: "Cannot find module '<my local computer dir name>/node_modules/.../something.js'"

I fixed this by modifying the actual node module(s) and replace those imports with:

var something = require('./something.js');

Is there a better way to handle this?

On a related note, for future reference, the pg package has a require('pg-native') that throws Cannot find module 'pg-native' from '[...]/node_modules/pg/lib/native' during deploy. That can be solved with the ignore configuration for babel.

Thanks!

thenikso avatar Mar 24 '16 18:03 thenikso

Thanks for this, @thenikso!

Could you please name a npm module which does require(__dirname + '/something.js')?

minibikini avatar Mar 25 '16 04:03 minibikini

I have this:

  • node_modules/pgpass/lib/index.js
  • node_modules/pg-types/lib/textParsers.js

(got them by npm installing pg-promise, same thing with pg-bricks)

thenikso avatar Mar 25 '16 09:03 thenikso

I had this problem with ajv module only on deploy. So I am back with serverless-optimizer-plugin that runs smoothly.

goncaloneves avatar Mar 31 '16 10:03 goncaloneves