Bada
Bada's Coding

Bada's Coding

(Yarn Berry) 'node' Command (X), 'yarn node' Command (O)

Bada's photo
Bada
·Nov 5, 2022·

1 min read

Please make sure you installed packages using Yarn Berry.

For example, if you want to run this file using this command,

|app.js|

const flatten = require('lodash.flatten');

const res = flatten([1, [2, 3]]);

console.log(res);
$ node app.js

You will get this error.

Error: Cannot find module 'lodash.flatten'

You have to run this code.

$ yarn node app.js

Then, you will get this result.

[ 1, 2, 3 ]

Be careful that the module system of |app.js| should be CommonJS.

If you want to use ES Module system like this file, you have to do two things.

|app.js|

import flatten from 'lodash.flatten';

const res = flatten([1, [2, 3]]);

console.log(res);

The first one is installing 'esm' package.

$ npm i esm

The second one is setting 'type' as 'module' in |package.json|.

{
  "type": "module"
}
 
Share this