Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

node@14.16.0 Error [ERR_REQUIRE_ESM] #471

Closed
GuoSirius opened this issue May 26, 2021 · 19 comments
Closed

node@14.16.0 Error [ERR_REQUIRE_ESM] #471

GuoSirius opened this issue May 26, 2021 · 19 comments

Comments

@GuoSirius
Copy link

node: 14.16.0
electron: 13.0.1
webpack: 4.46.0

import { Low, JSONFile } from 'lowdb'

const adapter = new JSONFile('example.json')

export const lowdb = new Low(adapter)

The above code run error, ERR_REQUIRE_ESM has been prompted, what is the solution?

image

@GuoSirius
Copy link
Author

https://pencilflip.medium.com/using-es-modules-with-commonjs-modules-in-node-js-1015786dab03

@jordan-loeser
Copy link

Convenient timing, I'm also seeing this issue.

@rafagets
Copy link

I have the same problem.

@istomyang
Copy link

Here some solutions:

  1. Download source code zip, change into "module": "commonjs" in tsconfig.json, run build and put project into your node_modules, you can remove src dict manually.
  2. Maintaining by youself and put into your packages if you are using lerna to manage your project.
  3. Hey, dear author. Could you publish a package with commonjs for my piteous ts-node, whose esm support is Experimental. Please, and thank you!

@giovannefeitosa
Copy link

I'm having the same issue here...

@Gitter499
Copy link

Same issue here, very annoying

@dankreiger
Copy link

dankreiger commented May 29, 2021

I opened a PR opened here to address this issue.


You can test it by installing the package from PR's lib branch

npm

npm install github:dankreiger/lowdb#chore/esm-cjs-hybrid-WITH-LIB 

yarn

yarn add github:dankreiger/lowdb#chore/esm-cjs-hybrid-WITH-LIB

@SystemFiles
Copy link

@dankreiger, god bless your soul

@Freddixx
Copy link

@dankreiger Nice one, thank you! Works like a charm!

@ste163
Copy link

ste163 commented May 31, 2021

@dankreiger Thank you! Working well :)

@ckcr4lyf
Copy link

ckcr4lyf commented Jun 3, 2021

Aces, thanks mate

@xdoer
Copy link

xdoer commented Jun 4, 2021

same problem

james2doyle added a commit to james2doyle/lowdb that referenced this issue Jun 4, 2021
Closes typicode#462. Closes typicode#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
@borsTiHD
Copy link

borsTiHD commented Jun 5, 2021

Same problem here. My workaround was rewriting my hole code to esm syntax (I'm glad I did that) and it works in my dev environment, but I have a new problem. I want to use webpack 5 to bundle my express+lowdb app. My bundled index.cjs uses require again and crashs because of lowdb Error [ERR_REQUIRE_ESM]: ....

Does anyone have a solution for me? Can I reconfigure webpack to build with "import" syntax?

webpack.config.js

import path from 'path'
import { fileURLToPath } from 'url'
import nodeExternals from 'webpack-node-externals'

const __dirname = path.dirname(fileURLToPath(import.meta.url))
const { NODE_ENV = 'production' } = process.env

export default {
    entry: './src/api/app.js',
    mode: NODE_ENV,
    // target: 'node',
    output: {
        path: path.resolve(__dirname, 'dist', 'server'),
        filename: 'index.js'
    },
    resolve: {
        extensions: ['.ts', '.js']
    },
    externalsPresets: { node: true }, // in order to ignore built-in modules like path, fs, etc.
    externals: [nodeExternals()] // in order to ignore all modules in node_modules folder
}

Edit:
I found a solution for my problem. I'm bundling lowdb.
Changing my webpack.config: externals: [nodeExternals({ allowlist: ['lowdb'] })]

@typicode
Copy link
Owner

typicode commented Jun 7, 2021

Closing as I prefer to keep v2 a pure ESM package. I'm aware not every project can migrate to ESM right now, but tools and libraries are catching up. If possible, I'd recommend migrating your project to ESM.

For anyone looking to fix the error, please follow this excellent guide:
https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c

@typicode typicode closed this as completed Jun 7, 2021
@carlos-burelo
Copy link

I have the same problem with typescript, but I can't use the "type": "module", because it doesn't recognize the ".ts" extension, any solution so far?

@Gitter499
Copy link

@carlos-burelo Make sure to have ESM modules configured in your tsconfig.json file. It should be on a property called module

@njfamirm
Copy link

@dankreiger thank you :))))

@i5hi
Copy link

i5hi commented Feb 16, 2022

@dankreiger thank you soo much!!!

@typicode please consider merging the PR that supports cjs. Most off your users have upvoted @dankreiger and down voted your choice to stick with pure ESM. Please consider majority users.

@i5hi
Copy link

i5hi commented Feb 16, 2022

I trust your judgement, as I am not fully aware of the implications of supporting cjs. If it doesnt create any breaking changes, please consider.

Thanks for your work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet