3

I'm trying to add a module to my project , following the doc I add this line to my index.js on my node.js project

import { bkLabs } from '@berkelium/nlp-core';

but I get the following error

SyntaxError: Cannot use import statement outside a module
    at wrapSafe (internal/modules/cjs/loader.js:1001:16)
    at Module._compile (internal/modules/cjs/loader.js:1049:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
    at Module.load (internal/modules/cjs/loader.js:950:32)
    at Function.Module._load (internal/modules/cjs/loader.js:790:12)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:75:12)
    at internal/main/run_main_module.js:17:47

then I looked for a solution and I found that I have to change the import for a require

const bkLabs = require('@berkelium/nlp-core');

but then I get this error:

internal/modules/cjs/loader.js:1102
      throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath);
  ^
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /home/user/proyectos/chatBot/node_modules/@berkelium/nlp-core/src/index.js
    require() of ES modules is not supported.
    require() of /home/user/proyectos/chatBot/node_modules/@berkelium/nlp-core/src/index.js from /home/user/proyectos/chatBot/index.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
    Instead rename /home/user/proyectos/chatBot/node_modules/@berkelium/nlp-core/src/index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /home/user/proyectos/chatBot/node_modules/@berkelium/nlp-core/package.json.
dr_fg2
  • 47
  • 1
  • 1
  • 5
  • 3
    https://stackoverflow.com/questions/58384179/syntaxerror-cannot-use-import-statement-outside-a-module – Teemu Jun 13 '22 at 12:01
  • In `package.json` set `"type": "module"` – mousetail Jun 13 '22 at 12:35
  • But If I user "type": "module" I get error from the oter libraries that I uses with require as express `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 '/home/user/proyectos/chatBot/package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.` – dr_fg2 Jun 13 '22 at 13:05
  • 1
    I spent several hours fighting this nasty problem. The solution adding "type": "module" in my package.json did not work for me. Instead, I had to create a new package.json file in the subdirectory where my *.js files were located with the single line {"type": "module"} and that worked. – NRS2000 Sep 19 '22 at 22:32
  • I had the same issue as @NRS2000, the proposed solution of creating a new package.json file in the subdirectory where the *.js files are located works, express is now running – Daniel Vieira Dec 12 '22 at 11:32

2 Answers2

3

I'm the developer of that library. Sorry for the inconvenience. You get that issue because the library was developed and packaged using ESModule syntax (i.e. you have to import modules using import). Since you are trying to use the library in a CommonJS syntax project, it won't work. The answer and the solution provided by Evgheni Calcutin is correct.

To resolve the issue I will work on the library so it can support both ESModule and CommonJS syntaxes. The library was renamed to make the name more convenient to pronounce. So the new library is berkelium. All documentations are provided.

Thanks!

uzluisf
  • 2,586
  • 1
  • 9
  • 27
Buddhi Kavindra
  • 171
  • 1
  • 9
2

Looks like the library you're trying to use is written with ESModule syntax and your NodeJS code with CommonJS.

You can’t require() ESM scripts; you can only import ESM scripts, like that: import {foo} from 'foo'.

This is a problem with mixing CommonJS and ESModules. By default, nodejs uses CommonJS module syntax unless you specify "type": "module" in package.json or using .mjs exentions.

Solution, choose one that works best for you:

  1. Write NodeJS in ESModule syntax
  2. Submit feature PR to @berkelium/nlp-core to support hybrid-module syntax (see more information here: https://nodejs.org/api/packages.html#dual-commonjses-module-packages)
Evgheni Calcutin
  • 332
  • 2
  • 14