elixir-nodejs icon indicating copy to clipboard operation
elixir-nodejs copied to clipboard

added package.json to priv specifying type: "commonjs"

Open Valian opened this issue 1 year ago • 0 comments

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.

Valian avatar Oct 08 '24 10:10 Valian