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

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

Open
cryptiklemur opened this issue May 16, 2019 · 17 comments
Open

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

cryptiklemur opened this issue May 16, 2019 · 17 comments

Comments

@cryptiklemur
Copy link

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:

nestjs/nest#2213

@cryptiklemur
Copy link
Author

cryptiklemur commented May 17, 2019

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
Copy link
Author

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

@cryptiklemur
Copy link
Author

Potentially solved by upgrading ncc to 0.19

@styfle styfle transferred this issue from another repository May 21, 2019
@styfle styfle added package issue priority Important issue or pull request to fast-track dynamic require labels May 21, 2019
@styfle
Copy link
Member

styfle commented May 21, 2019

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
Copy link
Author

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

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

@styfle
Copy link
Member

styfle commented May 22, 2019

@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
Copy link
Author

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

@cryptiklemur
Copy link
Author

image

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

@cryptiklemur
Copy link
Author

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

image

Note line: 53

@cryptiklemur
Copy link
Author

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

@styfle
Copy link
Member

styfle commented May 22, 2019

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
Copy link
Author

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

Getting the same error as #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
Copy link
Member

styfle commented May 22, 2019

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
Copy link
Author

cryptiklemur commented May 22, 2019

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
Copy link
Member

styfle commented May 24, 2019

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
Copy link
Author

Do you still get this with now dev?

@styfle
Copy link
Member

styfle commented May 24, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants