vercel / ncc

Compile a Node.js project into a single file. Supports TypeScript, binary addons, dynamic requires.
https://npmjs.com/@vercel/ncc
MIT License
9.27k stars 291 forks source link

npm package `optional` doesn't work on next-server #399

Open cryptiklemur opened 5 years ago

cryptiklemur commented 5 years ago

Found a problem while using nestjs. They have some stuff using the optional package, and it doesn't get transpiled properly by webpack

Made an issue over on their repo too:

https://github.com/nestjs/nest/issues/2213

cryptiklemur commented 5 years ago

Just to add on, when this package gets transpiled, it ends up looking like this:

/* 9297 */
/***/ (function(module) {

module.exports = function(module, options){
  try{
    if(module[0] in {".":1}){
      module = process.cwd() + module.substr(1);
    }
    return require(module);
  }catch(err){ 
    if (err.code !== "MODULE_NOT_FOUND" && options && options.rethrow) {
      throw err;
    }
  }
  return null;
};

/***/ }).

Note, require isn't a __webpack_require__ as expected.

cryptiklemur commented 5 years ago

Ideally, i'd like to specify that these two packages should be copied to the build in a node_modules directory?

cryptiklemur commented 5 years ago

Potentially solved by upgrading ncc to 0.19

styfle commented 5 years ago

I tested optional with the latest ncc (0.18.5) and it works.

mkdir example && cd example
yarn init -y
yarn add optional copee
echo "let op = require('optional'); let c = op('copee'); console.log(c);" > index.js
ncc build index.js 
node dist

This prints the copee functions as expected.

Can you provide steps to reproduce this issue?

cryptiklemur commented 5 years ago

Might potentially be a typescript specific issue. Reproducible repository here:

https://github.com/aequasi/typescript-ncc-optional-bug-report

styfle commented 5 years ago

@aequasi I get an error that graphql is missing. It reproduces when running tsc && node index.js.

You need to run npm install type-graphql graphql so that both are installed.

git clone https://github.com/aequasi/typescript-ncc-optional-bug-report
cd typescript-ncc-optional-bug-report
npm install graphql
ncc build index.ts 
node dist

This does not appear to be a bug with ncc so I'm going to close.

cryptiklemur commented 5 years ago

Please do not close this. Give me a minute to reply.

cryptiklemur commented 5 years ago

image

As you can see, type-graphql is installed via npm.

cryptiklemur commented 5 years ago

Further investigation into the dist/index.js code, shows that the require call is not transpiled

image

Note line: 53

cryptiklemur commented 5 years ago

Heres the dist/index.js file that was compiled: https://pastebin.com/vpuyErGL

styfle commented 5 years ago

As you can see, type-graphql is installed via npm.

Correct. But graphql is not installed.

Please try running your code without ncc and you will see that it fails in the same way.

cryptiklemur commented 5 years ago

Hmmm. that did change it. Perhaps there is something noticeable and reproducible here: https://github.com/secretary/api

Getting the same error as https://github.com/zeit/ncc/issues/377. While digging into the generated error, it led me to this optional package. If I alter the transpiled code, and console.log(optional('type-graphql')) (where its used), it leads to null. I've made sure that graphql and type-graphql are installed.

styfle commented 5 years ago

I tried the following:

git clone https://github.com/secretary/api
cd api
npm i
npm run dev

Then I get a CredentialsError.

Using ts-node version 8.1.0, typescript version 3.4.5
GraphQL schema file generated at: /Users/styfle/Code/foo/api/src/Module/schema.gql
[Nest] 27166   - 05/22/2019, 3:28 PM   [NestFactory] Starting Nest application...
[Nest] 27166   - 05/22/2019, 3:28 PM   [ExceptionHandler] Missing credentials in config +118ms
CredentialsError: Missing credentials in config

Is this what you are seeing?

cryptiklemur commented 5 years ago

No, sorry. forgot to remove some dependencies. Was connecting to external services. Removed that logic.

I get this:

{ Error: Generating schema error
    at Function.<anonymous> (/private/var/folders/_5/_7x_3yqn6kx16bd9sbl2k32w0000gn/T/zeit-fun-a8ffcc2c6cdeb/src/main.ts:238665:27)
    at Generator.next (<anonymous>)
    at fulfilled (/private/var/folders/_5/_7x_3yqn6kx16bd9sbl2k32w0000gn/T/zeit-fun-a8ffcc2c6cdeb/src/main.ts:327325:62)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7)
    at Function.Module.runMain (module.js:695:11)
    at startup (bootstrap_node.js:188:16)
    at bootstrap_node.js:609:3
  details: 
   [ { Type Query must define one or more fields.
    at SchemaValidationContext.reportError (/private/var/folders/_5/_7x_3yqn6kx16bd9sbl2k32w0000gn/T/zeit-fun-a8ffcc2c6cdeb/src/main.ts:23880:19)
    at validateFields (/private/var/folders/_5/_7x_3yqn6kx16bd9sbl2k32w0000gn/T/zeit-fun-a8ffcc2c6cdeb/src/main.ts:24099:13)
    at validateTypes (/private/var/folders/_5/_7x_3yqn6kx16bd9sbl2k32w0000gn/T/zeit-fun-a8ffcc2c6cdeb/src/main.ts:24062:9)
    at validateSchema (/private/var/folders/_5/_7x_3yqn6kx16bd9sbl2k32w0000gn/T/zeit-fun-a8ffcc2c6cdeb/src/main.ts:23844:3)
    at graphqlImpl (/private/var/folders/_5/_7x_3yqn6kx16bd9sbl2k32w0000gn/T/zeit-fun-a8ffcc2c6cdeb/src/main.ts:130646:61)
    at /private/var/folders/_5/_7x_3yqn6kx16bd9sbl2k32w0000gn/T/zeit-fun-a8ffcc2c6cdeb/src/main.ts:130621:250
    at new Promise (<anonymous>)
    at Object.graphql (/private/var/folders/_5/_7x_3yqn6kx16bd9sbl2k32w0000gn/T/zeit-fun-a8ffcc2c6cdeb/src/main.ts:130619:10)
    at Function.<anonymous> (/private/var/folders/_5/_7x_3yqn6kx16bd9sbl2k32w0000gn/T/zeit-fun-a8ffcc2c6cdeb/src/main.ts:238663:52)
    at Generator.next (<anonymous>) message: 'Type Query must define one or more fields.' } ] }

then hit: http://localhost:3000/graphql

styfle commented 5 years ago

It worked for me.

Steps

git clone https://github.com/secretary/api
cd api
npm i 
ncc build src/main.ts 
node dist

Output

GraphQL schema file generated at: /Users/styfle/Code/foo/api/dist/schema.gql
[Nest] 62010   - 05/23/2019, 8:11 PM   [NestFactory] Starting Nest application...
(node:62010) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.
[Nest] 62010   - 05/23/2019, 8:11 PM   [InstanceLoader] AppModule dependencies initialized +23ms
[Nest] 62010   - 05/23/2019, 8:11 PM   [InstanceLoader] GraphQLModule dependencies initialized +1ms
[Nest] 62010   - 05/23/2019, 8:11 PM   [RoutesResolver] IndexController {/}: +14ms
[Nest] 62010   - 05/23/2019, 8:11 PM   [RouterExplorer] Mapped {/, GET} route +6ms
[Nest] 62010   - 05/23/2019, 8:11 PM   [NestApplication] Nest application successfully started +83ms

Web

Visiting http://localhost:3000/graphql looks like this:

image

cryptiklemur commented 5 years ago

Do you still get this with now dev?

styfle commented 5 years ago

It didn't work with now dev because I don't have XCode installed.

But when I deploy I get a similar error to #377


GraphQL schema file generated at: /var/task/src/schema.gql
--
OUTPUT | May 24 2019 14:40:03:865 | /src/main.ts | [Nest] 1   - 2019-05-24 18:40   [NestFactory] Starting Nest application...
OUTPUT | May 24 2019 14:40:03:867 | /src/main.ts | (node:1) Warning: N-API is an experimental feature and could change at any time.
OUTPUT | May 24 2019 14:40:03:881 | /src/main.ts | [Nest] 1   - 2019-05-24 18:40   [InstanceLoader] AppModule dependencies initialized +16ms
OUTPUT | May 24 2019 14:40:03:882 | /src/main.ts | [Nest] 1   - 2019-05-24 18:40   [InstanceLoader] GraphQLModule dependencies initialized +1ms
OUTPUT | May 24 2019 14:40:03:887 | /src/main.ts | [Nest] 1   - 2019-05-24 18:40   [RoutesResolver] IndexController {/}: +5ms
OUTPUT | May 24 2019 14:40:03:890 | /src/main.ts | [Nest] 1   - 2019-05-24 18:40   [RouterExplorer] Mapped {/, GET} route +3ms
OUTPUT | May 24 2019 14:40:04:006 | /src/main.ts | { Error: Generating schema error     at Function.<anonymous> (/var/task/src/main.ts:114941:27)     at Generator.next (<anonymous>)     at fulfilled (/var/task/src/main.ts:209539:62)     at <anonymous>     at process._tickDomainCallback (internal/process/next_tick.js:228:7)     at Function.Module.runMain (module.js:695:11)     at startup (bootstrap_node.js:188:16)     at bootstrap_node.js:609:3   details:     [ { Type Query must define one or more fields.     at SchemaValidationContext.reportError (/var/task/src/main.ts:222921:19)     at validateFields (/var/task/src/main.ts:223140:13)     at validateTypes (/var/task/src/main.ts:223103:9)     at validateSchema (/var/task/src/main.ts:222885:3)     at graphqlImpl (/var/task/src/main.ts:465526:61)     at /var/task/src/main.ts:465501:250     at new Promise (<anonymous>)     at Object.graphql (/var/task/src/main.ts:465499:10)     at Function.<anonymous> (/var/task/src/main.ts:114939:52)     at Generator.next (<anonymous>) message: 'Type Query must define one or more fields.' } ] }

image