-1

sorry this is a very basic question. I wrote some functions to handle requests by passing a callback to http.createServer. The functions work if I import them directly from the functions.js file like in this code.

import {handle_request} from './functions.js';
import http from 'http';

http.createServer(handle_request).listen(8080);

Now I want to put this in a package, so that I can install the libraries my code depends on automatically with npm install. My package.json looks like this, I can install the package and I's found in the node REPL, but how do I actually call the functions in the package or use them as callbacks?

{
  "name": "package_name",
  "version": "1.0.0",
  "description": "posts images to a slack channel",
  "main": "functions.js",
  "author": "me",
  "dependencies": {
    "mustache": "*"
  },
  "type": "module"
}

The functions are defined like this:

export var handle_request = function (req, res) {
// function body ...
}

I think my problem is either the import statements in the script that calls the library or some missing exports in the package.

Sorry again for the very basic question, I found many posts and documentations on npm packages but nothing solving my problem. Any help appreciated.

snaut
  • 2,261
  • 18
  • 37
  • `import {handle_request} from 'package_name';` – Heiko Theißen Jan 16 '23 at 16:21
  • I want to use this in a script outside of the package. If I use this syntax I get "Cannot use import statement outside a module" – snaut Jan 16 '23 at 16:34
  • Now I get `Error [ERR_REQUIRE_ESM]: require() of ES Module [...]/functions.js from [...]/app/start_server.js not supported. Instead change the require of functions.js in [...]/app/start_server.js to a dynamic import() which is available in all CommonJS modules.` (I have other imports inside functions.js, can this be a problem, if I'm mixing import mechanisms) – snaut Jan 16 '23 at 16:38
  • 1
    See the examples [here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import). – Heiko Theißen Jan 16 '23 at 16:48
  • 1
    Does this answer your question? [SyntaxError: Cannot use import statement outside a module](https://stackoverflow.com/questions/58384179/syntaxerror-cannot-use-import-statement-outside-a-module) – Konrad Jan 16 '23 at 16:51
  • Thank you @HeikoTheißen I found what I was missing in the MDN article. Import gives a promise to the module not the module itself, with `let module_name = await import("module_name");` it works. – snaut Jan 16 '23 at 18:02

1 Answers1

0

Posting the solution for sake of completeness. There were two mistakes I made:

  1. there's a difference between import mechanisms inside and outside of a module context. The script calling the functions is not inside the module, therefore a using dynamic import is more appropriate.

  2. dynamic import returns a promise, which I missed on an earlier try. In my case this is only called before the server starts, so I can just wait for the promise to be evaluated.

What worked in the end was:

let package_name = await import("package_name");

Thanks for the comment with the link to mdn, this helped a lot.

snaut
  • 2,261
  • 18
  • 37