Gopal S Akshintala
Gopal S Akshintala

Reputation: 1973

How can I use globally installed Volta package in Node script?

I installed jsonlint globally with volta install jsonlint, but when trying to execute a node script with node json.js as below, it is not recognized

A standalone Script (not part of any module):

var Jsonlint = require("jsonlint")
const jsonString = '{"name": "John", "age": 30, "city": "New York"}';

JsonLint.format(jsonString, (err, formattedJson) => {
  if (err) {
    console.error(err);
  } else {
    console.log(formattedJson);
  }
});

Error:

node:internal/modules/cjs/loader:1147
  throw err;
  ^

Error: Cannot find module 'jsonlint'
Require stack:
- /Users/gopala.akshintala/Desktop/json.js
    at Module._resolveFilename (node:internal/modules/cjs/loader:1144:15)
    at Module._load (node:internal/modules/cjs/loader:985:27)
    at Module.require (node:internal/modules/cjs/loader:1235:19)
    at require (node:internal/modules/helpers:176:18)
    at Object.<anonymous> (/Users/gopala.akshintala/Desktop/json.js:1:16)
    at Module._compile (node:internal/modules/cjs/loader:1376:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1435:10)
    at Module.load (node:internal/modules/cjs/loader:1207:32)
    at Module._load (node:internal/modules/cjs/loader:1023:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:135:12) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [ '/Users/gopala.akshintala/Desktop/json.js' ]
}

But I can see jsonlint is recognized in terminal globally:

$ jsonlint -v
1.6.3

I am expecting that require should pick jsonlint from here

$ which jsonlint
~/.volta/bin/jsonlint

What am I missing?

Upvotes: 0

Views: 370

Answers (0)

Related Questions