Once you have installed a package in node_modules, you can use it in your code.

Using unscoped packages in your projects

Node.js module

If you are creating a Node.js module, you can use a package in your module by passing it as an argument to the require function.

var lodash = require('lodash');
var output = lodash.without([1, 2, 3], 1);
console.log(output);

package.json file

In package.json, list the package under dependencies. You can optionally include a semantic version.

{
"dependencies": {
"package_name": "^1.0.0"
}
}

Using scoped packages in your projects

To use a scoped package, simply include the scope wherever you use the package name.

Node.js module

var projectName = require("@scope/package-name")

package.json file

In package.json:

{
"dependencies": {
"@scope/package_name": "^1.0.0"
}
}

Resolving "Cannot find module" errors

If you have not properly installed a package, you will receive an error when you try to use it in your code. For example, if you reference the lodash package without installing it, you would see the following error:

module.js:340
throw err;
^
Error: Cannot find module 'lodash'
  • For scoped packages, run npm install <@scope/package_name>
  • For unscoped packages, run npm install <package_name>
Edit this page on GitHub
1 contributormona
Last edited by mona on March 21, 2023
声明:npm 及相关 logo 的版权归 npmjs.com 所有。本站点仅用于 npm 中文文档,与 npmjs.com 没有任何关系。由于译者水平有限,且避免产生误解,条款和政策内容不进行翻译,关于这部分,请移步官网查看最新内容。