From 5651fff2ce31e69769f31d7690356fc98d1b8bd7 Mon Sep 17 00:00:00 2001 From: Valian Date: Tue, 8 Oct 2024 12:46:22 +0200 Subject: [PATCH 1/2] added package.json to priv specifying CJS type explicitly In my Elixir project, I have package.json in a root directory. It specifies `type: module` as I'm writing JS in ESM syntax, with import / export. With that setup, nodejs treats `deps/nodejs/priv/server.js` as a project script, since it doesn't have package.json next to it. Exact error I'm getting: ``` ReferenceError: require is not defined in ES module scope, you can use import instead This file is being treated as an ES module because it has a '.js' file extension and '/Users/user/project/package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension. ``` Adding package.json to priv simply explicitly informs nodejs that script is written in CJS. --- priv/package.json | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 priv/package.json diff --git a/priv/package.json b/priv/package.json new file mode 100644 index 0000000..28a8087 --- /dev/null +++ b/priv/package.json @@ -0,0 +1,4 @@ +{ + "name": "elixir-nodejs", + "type": "commonjs" +} \ No newline at end of file From a87c6f55dda1743db37d57cb88a557b1155ccbfc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ska=C5=82ecki?= Date: Mon, 2 Dec 2024 23:33:29 +0100 Subject: [PATCH 2/2] Add newline at the end of package.json --- priv/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/priv/package.json b/priv/package.json index 28a8087..f7c3ad9 100644 --- a/priv/package.json +++ b/priv/package.json @@ -1,4 +1,4 @@ { "name": "elixir-nodejs", "type": "commonjs" -} \ No newline at end of file +}