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

TS2305: Module '"[...]node_modules/@improbable-eng/grpc-web/dist/grpc-web-client.umd"' has no exported member 'grpc'. #369

Closed
nothingrandom opened this issue Mar 8, 2019 · 25 comments

Comments

@nothingrandom
Copy link

nothingrandom commented Mar 8, 2019

This is probably a mistake my side, and it's 4pm Friday so we'll see if I can fix it after the beers make an appearance - but I thought I'd make a cry for help also!

package.json

"dependencies": {
    "@improbable-eng/grpc-web": "0.9.1",
    "@types/google-protobuf": "^3.2.5",
    "google-protobuf": "^3.2.0",

service / request user.ts file
import {grpc} from "@improbable-eng/grpc-web";

Full browser log

./src/services/user.ts
Module build failed (from ./node_modules/ts-loader/index.js):
Error: Typescript emitted no output for [...]src/services/user.ts.
    at successLoader ([...]node_modules/ts-loader/dist/index.js:41:15)
    at Object.loader ([...]node_modules/ts-loader/dist/index.js:21:12)
console.(anonymous function) @ console.js?00ac:35
errors @ client?81da:168
onmessage @ socket.js?e29c:41
EventTarget.dispatchEvent @ sockjs.js?9be2:170
(anonymous) @ sockjs.js?9be2:887
SockJS._transportMessage @ sockjs.js?9be2:885
EventEmitter.emit @ sockjs.js?9be2:86
WebSocketTransport.ws.onmessage @ sockjs.js?9be2:2961
console.js?00ac:35 [...]src/services/user.ts
./src/services/user.ts
[tsl] ERROR in [...]src/services/user.ts(1,9)
      TS2305: Module '"[...]node_modules/@improbable-eng/grpc-web/dist/grpc-web-client.umd"' has no exported member 'grpc'.

Module import folder

[ '|-- node_modules',
  '    |-- grpc-web',
  '        |-- LICENSE.txt',
  '        |-- README.md',
  '        |-- package.json',
  '        |-- dist',
  '            |-- grpc-web-client.js',
  '            |-- grpc-web-client.umd.js',
  '            |-- typings',
  '                |-- ChunkParser.d.ts',
  '                |-- Code.d.ts',
  '                |-- client.d.ts',
  '                |-- debug.d.ts',
  '                |-- detach.d.ts',
  '                |-- index.d.ts',
  '                |-- invoke.d.ts',
  '                |-- message.d.ts',
  '                |-- metadata.d.ts',
  '                |-- service.d.ts',
  '                |-- unary.d.ts',
  '                |-- util.d.ts',
  '                |-- transports',
  '                    |-- Transport.d.ts',
  '                    |-- http',
  '                    |   |-- fetch.d.ts',
  '                    |   |-- http.d.ts',
  '                    |   |-- xhr.d.ts',
  '                    |   |-- xhrUtil.d.ts',
  '                    |-- websocket',
  '                        |-- websocket.d.ts',
  '' ]
  • Vue app
  • Yarn not npm
@jonny-improbable
Copy link
Contributor

First thing I note is that you are consuming the .umd file which is primarily intended to be used in browsers. Are you pre-processing your files with anything (ie: Webpack, rollup?).

@nothingrandom
Copy link
Author

@jonny-improbable Yes, I too thought that was a little weird.
Webpack, as this is within a vue project. Created using the Vue CLI which runs a webpack configuration > https://cli.vuejs.org/

@jonny-improbable
Copy link
Contributor

@nothingrandom I'd dig into why it's picking up the .umd file as a starting point; maybe try deleting the "browser": "dist/grpc-web-client.umd.js" entry in your local copy of grpc-web's package.json

@nothingrandom
Copy link
Author

nothingrandom commented Mar 8, 2019

@jonny-improbable Weirdly that gives me an error with the other file instead / as well

TS2306: File '[...]/node_modules/@improbable-eng/grpc-web/dist/grpc-web-client.js' is not a module.

@jonny-improbable
Copy link
Contributor

Ah, I meant removing the line. Are you able to create a minimal repro for this?

@nothingrandom
Copy link
Author

Yes, I did remove just that one line. The file now looks like this.

{
  "name": "@improbable-eng/grpc-web",
  "version": "0.9.1",
  "description": "gRPC-Web client for browsers (JS/TS)",
  "main": "dist/grpc-web-client.js",
  "types": "dist/typings/index.d.ts",
  "scripts": {
    "clean": "rm -rf dist",
    "postbootstrap": "npm run lib:build",
    "lib:build": "npm run clean && webpack"
  },
  "author": "Improbable",
  "license": "Apache-2.0",
  "repository": {
    "type": "git",
    "url": "github.com/improbable-eng/grpc-web"
  },
  "keywords": [
    "grpc",
    "grpc-web",
    "protobuf",
    "typescript",
    "ts"
  ],
  "files": [
    "dist"
  ],
  "peerDependencies": {
    "google-protobuf": "^3.2.0"
  },
  "dependencies": {
    "browser-headers": "^0.4.0"
  },
  "devDependencies": {
    "@types/google-protobuf": "^3.2.5",
    "@types/node": "^7.0.5",
    "google-protobuf": "^3.2.0",
    "ts-loader": "^4.5.0",
    "typescript": "3.3.3",
    "webpack": "^4.29.3",
    "webpack-cli": "^3.2.3"
  },
  "gitHead": "a11641a4c05dcb97c06a36a62a0bcb98772dfb71"
}

I am able to do so, but it's not going to be quick. This is quite a heavy repo currently

@nothingrandom
Copy link
Author

If things still aren't clear, I'm happy to make a test vue cli app using a basic import for this and share it? Might have a chance over the weekend, most likely Monday. If it'd help?

@jonnyreeves
Copy link
Contributor

jonnyreeves commented Mar 8, 2019 via email

@nothingrandom
Copy link
Author

Gonna ensure that's not the case here; I'll have to find a cause eventually. Internal product that's far too deep to switch to React / other and requires access to stream.

I'll get that sorted quickly. Ought I try base it off of grpc-web-react-example so that it can be useful for others in the future looking at this from a Vue / webpack POV?

@jonny-improbable
Copy link
Contributor

jonny-improbable commented Mar 8, 2019 via email

@nothingrandom
Copy link
Author

Hey, couple of questions whilst I'm looking at my root repo. It appears it's aiming for the grpc-web-client.js / grpc-web-client.umd.js files. Is this correct? The reason for the TS2306 is that the .js file doesn't have any named exports, see https://stackoverflow.com/questions/32805559/typescript-es6-import-module-file-is-not-a-module-error

Is the import I make, aiming to hit the dist/typings/index.d.ts file? Or maybe the minification on the client file is causing me issues with webpack?

@nothingrandom
Copy link
Author

@jonny-improbable @jonnyreeves. Got the test project "working" (gives the same error, but I guess that's a good thing?)

https://github.com/nothingrandom/grpc-vue-test-project

Let me know if you have any questions, some things are a little different to the react example you have; but they can all get sorted / aligned later

@jonny-improbable
Copy link
Contributor

Morning @nothingrandom; thanks for the repro! I will try to take a look this week, work schedule permitting, although as always I'd welcome input from anyone else who has the time to dig in.

@ypoluektovich
Copy link

I encountered the same problem today when I tried to upgrade the grpc-web dependency.
My project is written in ES6 and transpiled to ES5 by rollup.
I tried to import grpc like this:

import { grpc } from '@improbable-eng/grpc-web';

It also decided to use the umd file and couldn't find the export.
I looked into the file and this is what it looks like (formatting is mine):

!function (e, t) {
	if ('object' == typeof exports && 'object' == typeof module) {
		module.exports = t();
	} else if ('function' == typeof define && define.amd) {
		define([], t);
	} else {
		var n = t();
		for (var r in n) ('object' == typeof exports ? exports : e)[r] = n[r];
	}
}(window, function () { ... });

Apparently, 'object' == typeof exports && 'object' == typeof module is not true in my case, even though I'd like it to be. And obviously setting things to window doesn't propagate to other modules' imports.
Now, I have no idea how that file is generated and why it is what it is, so my workaround is to add a build step that augments this file (using rollup-plugin-hypothetical) using this template:

const window = {};
${oldContents}
export const grpc = window.grpc;

Hope this helps somehow.

@nothingrandom
Copy link
Author

@ypoluektovich; Looks like it could be a useful hotfix. What file are you adding that to?

@ypoluektovich
Copy link

@nothingrandom Just the node_modules/@improbable-eng/grpc-web/dist/grpc-web-client.umd.js file. We don't patch it on disk though, instead we configured our rollup invocation to think that the file has different contents. Specifically, we read the file from the disk, add the first and last line as specified in the template, then put the resulting string into a property on the configuration object. Our build system is just a few JS files that we run via node ./src/build/build.js, so I doubt our code will be useful to you.

@nothingrandom
Copy link
Author

@ypoluektovich; you're somewhat correct. Useful but not a viable implementation for us. It also seems a bit "hotfix" and it'd be nice to have the package work as expected. Thanks for checking this out for me though

@nothingrandom
Copy link
Author

Hey @jonny-improbable. Did you manage to get around to having a look at this?

@jonny-improbable
Copy link
Contributor

jonny-improbable commented Apr 15, 2019 via email

@graup
Copy link

graup commented May 21, 2019

I have a similar issue with rollup. I had to define a namedExport to make this work withrollup-plugin-commonjs, otherwise it throws the same error as in the OP. This is for a browser (umd) build.

Something like

commonjs({
    namedExports: {'path/to/node_modules/@improbable-eng/grpc-web/dist/grpc-web-client.umd.js': ['grpc']}
}),

@rmilejcz
Copy link

rmilejcz commented Jul 3, 2019

Having the same error as well via rollup, I had to slightly tweak @graup's fix (grpc-web-client.js instead of grpc-web-client.umd.js also a slight syntax error) but it is building now:

commonjs({
  namedExports: {
    './node_modules/@improbable-eng/grpc-web/dist/grpc-web-client.js': [
      'grpc',
    ],
  },
}),

@zaucy
Copy link

zaucy commented Jul 15, 2019

I'm getting this error as well while trying to compile with rollup. Specifically with the bazel rule rollup_bundle from bazelbuild/rules_nodejs.

Error: 'grpc' is not exported by external\npm\node_modules\@improbable-eng\grpc-web\dist\grpc-web-client.js

@awmichel
Copy link

awmichel commented Dec 2, 2019

I ran into this working on Dig-Doug/rules_typescript_proto#13 as well. It looks like the examples use a ES module syntax import { grpc } from '@improbable-eng/grpc-web;`, but Webpack is generating a CommonJS export.

libraryTarget: 'commonjs',

I believe historically tools like Webpack/Rollup gracefully handled this, however lately it seems more and more are refusing the transparently convert between CommonJS/ESM formats.

Configuring Webpack to generate a ESM lib and adding "module": "dist/grpc-web-client.mjs", to the package.json should do the trick. It's also possible generating a .mjs version alone will work, but adding the module declaration to the package file can't hurt.

References:
ES Module Reference
Hybrid NPM Packages Blog Post

@nothingrandom
Copy link
Author

This is fixed, somehow in 0.11.0 which we recently added to our repos.

@johanbrandhorst
Copy link
Contributor

Cool!

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

9 participants