Home > OS >  Import module in nodejs
Import module in nodejs

Time:12-08

I am trying to import a node module in node 12.22.5

const Gpio = require('pigpio').Gpio;

But I get an error telling me that require is not defined

CodePudding user response:

It happens when you declare your package type as module in your package.json. If you do this, certain CommonJS variables can't be used, including require.

To fix this, remove "type": "module" from your package.json and make sure you don't have any files ending with .mjs.

CodePudding user response:

For Raspberry Pi, you first need to install the module, then you can use it:

npm install pigpio
  • Related