typicode / lowdb

Simple and fast JSON database
MIT License
21.49k stars 923 forks source link

Support "main" index file for non-module environments and flat import #475

Closed james2doyle closed 3 years ago

james2doyle commented 3 years ago

Closes #462. Closes #471.

Implements "main" which is required when your primary export file is not <root>/index.js. See https://docs.npmjs.com/cli/v7/configuring-npm/package-json#main

typicode commented 3 years ago

Thanks for the PR :+1: I'd rather keep lowdb a pure ESM package.

james2doyle commented 3 years ago

That's disappointing. This one line fixes most people's problems with this working in other environments. This is just a config change. No code updates required.

On Sun., Jun. 6, 2021, 6:26 p.m. typicode, @.***> wrote:

Closed #475 https://github.com/typicode/lowdb/pull/475.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/typicode/lowdb/pull/475#event-4848962902, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAK37GBZYLI3UTVXKLJIVRTTRQN2RANCNFSM46DHP4CQ .

typicode commented 3 years ago

Sorry. That said, I'm not sure having just main is sufficient (at least just changing that wasn't enough in my tests).

$ npm init -y
$ npm i lowdb
$ echo "require('lowdb')" > index.js

# edit node_modules/lowdb/package.json
# and add "main": ""main": "./lib/index.js"

$ node index.js
node:internal/modules/cjs/loader:1126
      throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath);
      ^

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /tmp/foo/node_modules/lowdb/lib/index.js
require() of ES modules is not supported.
james2doyle commented 3 years ago

It still won't support node without experimental modules enabled. But it will work properly in typescript or when using babel to compile the modules since it knows where to find the main index file

On Sun., Jun. 6, 2021, 7:09 p.m. typicode, @.***> wrote:

Sorry. That said, I'm not sure having just main is sufficient (at least just changing that wasn't enough in my tests).

$ npm init -y $ npm i lowdb $ echo "require('lowdb')" > index.js

edit node_modules/lowdb/package.json# and add "main": ""main": "./lib/index.js"

$ node index.js node:internal/modules/cjs/loader:1126 throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath); ^

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /tmp/foo/node_modules/lowdb/lib/index.jsrequire() of ES modules is not supported.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/typicode/lowdb/pull/475#issuecomment-855526681, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAK37GEUPTEBWNZCK7TXTLTTRQS5BANCNFSM46DHP4CQ .