octokit.js: Webpack targeting node wrongly pick up @octokit/request/dist-web

Hello, I have an issue when packaging for node.

package.js:

{
  "scripts": {
    "build": "webpack --target node -p"
  },
  "dependencies": {
    "@octokit/rest": "^16.30.1"
  }
}

src/index.js:

const Octokit = require("@octokit/rest");
const octokit = new Octokit();

octokit.repos
  .listForOrg({
    org: "octokit",
    type: "public"
  })
  .then(({ data }) => {
	console.log(data);
  });

Running node src/index.js works fine.

But running yarn build output this:

  % yarn build
yarn run v1.17.3
warning package.json: No license field
$ webpack --target node -p
Hash: b3aadfcaa886809054ff
Version: webpack 4.39.3
Time: 1960ms
Built at: 09/25/2019 7:13:28 PM
  Asset     Size  Chunks             Chunk Names
main.js  199 KiB       0  [emitted]  main
Entrypoint main = main.js
 [0] external "stream" 42 bytes {0} [built]
 [2] external "zlib" 42 bytes {0} [built]
 [9] external "url" 42 bytes {0} [built]
[10] external "http" 42 bytes {0} [built]
[14] external "https" 42 bytes {0} [built]
[15] ./src/index.js 390 bytes {0} [built]
[61] ./node_modules/@octokit/request/dist-web/index.js + 4 modules 58.2 KiB {0} [built]
     |    5 modules
    + 55 hidden modules
✨  Done in 3.02s.

And running the resulting bundle with node dist/main.js return this error:

  % node dist/main.js                                                                          190925-191328:62244
/dist/main.js:1
ReferenceError: navigator is not defined
    at i (/dist/main.js:1)
    at Module.<anonymous> (/dist/main.js:15:4335)
    at t (/dist/main.js:1)
    at Object.<anonymous> (/dist/main.js:1)
    at t (/dist/main.js:1)
    at Object.<anonymous> (/dist/main.js:1)
    at t (/dist/main.js:1)
    at Object.<anonymous> (/dist/main.js:1)
    at t (/dist/main.js:1)
    at Object.<anonymous> (/dist/main.js:1)

Sure I’m doing something wrong, but I’m wondering in the webpack logs there is this line:

[61] ./node_modules/@octokit/request/dist-web/index.js + 4 modules 58.2 KiB {0} [built]

And looking into node_modules/@octokit/request there is also a dist-node, so I’m wondering if the error is because webpack is picking dist-web and if yes why so as you can see the webpack command is targeting node.

Thank you so much!

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 5
  • Comments: 18 (8 by maintainers)

Commits related to this issue

Most upvoted comments

No thanks, I’m working on another solution to the problem. Please bear with me

There are some more workarounds described at https://github.com/gr2m/universal-user-agent/issues/23, I’ve also posted an update there. The good news is we have have a standard way to handle this use case coming from Node.js new module system. And I also know that the folks from Pika are looking into implementing the new standard. I cannot give you an ETA, but a fix is coming.

For the time being I recommend to use one of the described workarounds