Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

osapi fails to build (depends on noda) #368

Open
reetou opened this issue Apr 29, 2019 · 7 comments
Open

osapi fails to build (depends on noda) #368

reetou opened this issue Apr 29, 2019 · 7 comments

Comments

@reetou
Copy link

reetou commented Apr 29, 2019

Hello, I cannot deploy my backend lambda function because of strange error and I want to ask for your help if it's possible.

General issue is I'm using osapi for my s3 connections and stuff and this package works well while I'm working locally but since it's deployed backend sends 502 on any lambda where piece of code with requiring osapi is present.

I created a public deployment: https://zeit.co/kkz-zae/express/g081kykjq
and also GitHub repository to reproduce this:
https://github.com/reetou/now-reproduce-express

As far as I understand it cannot find some dependencies of osapi for some reason.

Hope you will help me or feed my interest why it happened at least.

@rauchg
Copy link
Member

rauchg commented Apr 29, 2019

@reetou can you try bundling a simple function that you run through ncc that minimally reproduces?

npm i -g @zeit/ncc

https://github.com/zeit/ncc

@reetou
Copy link
Author

reetou commented Apr 29, 2019

@rauchg Of course, here it is: link to commit
Actually reproducing example is lying under s3 directory in repository

@reetou
Copy link
Author

reetou commented Apr 29, 2019

Here's output:

/Users/vladimirsinitsyn/git/express/dist/index.js:463
            throw ex;
            ^

Error: Cannot find module '/Users/vladimirsinitsyn/git/express/swift'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:594:15)
    at Function.Module._load (internal/modules/cjs/loader.js:520:25)
    at Module.require (internal/modules/cjs/loader.js:650:17)
    at require (internal/modules/cjs/helpers.js:20:18)
    at Object.inRequire (/Users/vladimirsinitsyn/git/express/dist/index.js:457:15)
    at Object.857 (/Users/vladimirsinitsyn/git/express/dist/index.js:930:20)
    at __webpack_require__ (/Users/vladimirsinitsyn/git/express/dist/index.js:22:30)
    at Object.86 (/Users/vladimirsinitsyn/git/express/dist/index.js:136:15)
    at __webpack_require__ (/Users/vladimirsinitsyn/git/express/dist/index.js:22:30)
    at startup (/Users/vladimirsinitsyn/git/express/dist/index.js:36:19)

And probably problem lies here
osapi/index.js

const MODULE_REQUIRE = 1
    /* built-in */
    
    /* NPM */
    , noda = require('noda')
    , cloneObject = require('jinang/cloneObject')
    
    /* in-package */
    , s3 = noda.inRequire('s3')
    , swift = noda.inRequire('swift')
    ;

Looks like these noda.inRequire that were used in osapi to require certain files inside project were interpreted wrong or kind of.

/**
 * To require some sub module in same package with fixed subpath wherever the caller is located.
 *
 * @example
 * // PACKAGE_HOMEDIR/lib/index.js
 *
 * // PACKAGE_HOMEDIR/foo.js
 * noda.inRequire('lib');
 *
 * // PACKAGE_HOMEDIR/foo/bar.js
 * noda.inRequire('lib');
 *
 * @param {string} subpath sub module's path relative to the home directory of the package in which the caller is located.
 */
let inRequire = function(subpath, nullIfNotFound) {
    // Find home directory of the package in which the caller is located.
    let dirname = getCallerPackageDir();
    let pathname = path.join(dirname, subpath);

    let mod = null;
    try {
        mod = require(pathname);
    } catch (ex) {
        if (nullIfNotFound && ex.code === 'MODULE_NOT_FOUND') {
            mod = null;
        }
        else {
            throw ex;
        }
    }
    return mod;
};

Or by looking on code above I can think that noda.inRequire function tries to require file by subpath variable and since it's built with webpack path to dir is something like './' and it's just tries to find swift file or directory which is unavailable since code is built with ncc to a single node.js file.

@styfle styfle transferred this issue from another repository Apr 30, 2019
@styfle styfle changed the title [@now/node] 502 error - Module not located in any node.js package osapi fails to build (depends on noda) Apr 30, 2019
@styfle styfle added package issue priority Important issue or pull request to fast-track labels Apr 30, 2019
@styfle
Copy link
Member

styfle commented Apr 30, 2019

@reetou Thanks for the bug report!

As you mentioned, the osapi package depends on noda which performs a dynamic require and ncc has trouble finding the required files during static analysis.

I moved this issue to the ncc repo for tracking purposes.

@guybedford
Copy link
Contributor

The only way to support this really is through a special casing due to the indirect dynamic require. noda only has 188 downloads per week, which seems quite small to create a special case, but we could do it.

@reetou
Copy link
Author

reetou commented Apr 30, 2019

@guybedford Thanks for your response. I already rewrote this part of code with aws-sdk so this is no more the case for me.

@reetou reetou closed this as completed Apr 30, 2019
@reetou
Copy link
Author

reetou commented Apr 30, 2019

Sorry, closed this by mistake.

@reetou reetou reopened this Apr 30, 2019
@styfle styfle removed the priority Important issue or pull request to fast-track label Apr 30, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants