TheThingsArchive / node-app-sdk

The Things Network Application SDK for Node.JS
https://www.thethingsnetwork.org/docs/node-js/
MIT License
41 stars 28 forks source link

SyntaxError: The requested module 'ttn' does not provide an export named 'data' #99

Closed sbrl closed 4 years ago

sbrl commented 5 years ago

Currently if I use this package with Node.js, I get the following error:

file://path/to/Msc-Summer-Project/server/ttn-app-server/TTNAppServer.mjs:3
import { data as ttn_data } from 'ttn';
         ^^^^
SyntaxError: The requested module 'ttn' does not provide an export named 'data'
    at ModuleJob._instantiate (internal/modules/esm/module_job.js:93:21)
    at async ModuleJob.run (internal/modules/esm/module_job.js:108:20)
    at async Loader.import (internal/modules/esm/loader.js:128:24)

I'm executing my program like this: node --experimental-modules ./server/index.mjs

....I think there's a packaging error. Inspecting index.js reveals that it's using some custom exports variable, and not an ES6 export { example_function }; statement as it should be, or even a legacy module.exports = { /* ..... */ }; - hence Node.js doesn't recognise it.

Edit: Oh yeah, here are my software version details:

$ npm --version
6.9.2
$ node --version
v12.5.0
johanstokking commented 5 years ago

Thanks for reporting.

You mean this is wrong?

sbrl commented 5 years ago

Huh, that looks completely different to what I'm seeing in the package I've installed from npm, @johanstokking! Here's what I'm seeing: index.js.log (renamed to .log so that GitHub accepts it)

Specifically, this:

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.account = exports.data = exports.application = exports.services = exports.Discovery = exports.ApplicationClient = exports.wildcard = exports.DataClient = exports.key = exports.HandlerClient = undefined;

....

var data = exports.data = function () {
  var _ref3 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee3(appID, accessKeyOrToken, opts) {
    var handler;
.....
johanstokking commented 5 years ago

Hmm 🤔 the published version may be transpiled weirdly.

What if you clone this repo and reference locally?

If you have ideas to fix this, that would be very much welcomed.

sbrl commented 5 years ago

@johanstokking After cloning it to a subfolder inside node_modules and running the following test program:

"use strict";

import { data } from './node_modules/node-app-sdk/src/index.js';

console.log(data);

....I get this error:

$ node --experimental-modules index.mjs 
(node:7124) ExperimentalWarning: The ESM module loader is experimental.
file:///tmp/test/index.mjs:1
import { data } from './node_modules/node-app-sdk/src/index.js';
         ^^^^
SyntaxError: The requested module './node_modules/node-app-sdk/src/index.js' does not provide an export named 'data'
    at ModuleJob._instantiate (internal/modules/esm/module_job.js:93:21)
    at async ModuleJob.run (internal/modules/esm/module_job.js:108:20)
    at async Loader.import (internal/modules/esm/loader.js:134:24)

For reference, I'm attempting to import src/index.js.

If I change the above test script to import the default export instead, I get this error:

$ node --experimental-modules index.mjs 
(node:8388) ExperimentalWarning: The ESM module loader is experimental.
/tmp/test/node_modules/node-app-sdk/src/index.js:6
import { HandlerClient } from "./handler"
       ^

SyntaxError: Unexpected token {
    at Module._compile (internal/modules/cjs/loader.js:720:23)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
    at Module.load (internal/modules/cjs/loader.js:643:32)
    at Function.Module._load (internal/modules/cjs/loader.js:556:12)
    at internal/modules/esm/translators.js:87:15
    at Object.meta.done (internal/modules/esm/create_dynamic_module.js:48:9)
    at file:///tmp/test/node_modules/node-app-sdk/src/index.js:9:13
    at ModuleJob.run (internal/modules/esm/module_job.js:111:37)
    at async Loader.import (internal/modules/esm/loader.js:134:24)

....I'm confused.

Minyanaing commented 5 years ago

Instead of "import", use const and require. const { data, application } = require('ttn');

sbrl commented 5 years ago

In theory that would work around the issue @Minyanaing, the but specific problem here is that it doesn't work with es6 modules - i.e. the import export syntax.

johanstokking commented 5 years ago

@Minyanaing @sbrl can you make a PR?

sbrl commented 5 years ago

@johanstokking I would investigate doing so, but read this. It's an announcement about es6 modules on npm.