Movatterモバイル変換


[0]ホーム

URL:


Skip to searchSkip to content

Site navigation

See DetailsTable of contents

Once you haveinstalled a package innode_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 therequire function.

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

package.json file

Inpackage.json, list the package under dependencies. You can optionally include asemantic 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

Inpackage.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 thelodash package without installing it, you would see the following error:

module.js:340
throw err;
^
Error: Cannotfind module'lodash'
  • For scoped packages, runnpm install <@scope/package_name>
  • For unscoped packages, runnpm install <package_name>
Edit this page on GitHub
3 contributorslukekarryshuzejrovicaethomson
Last edited bylukekarrys onOctober 23, 2023

Table of contents


[8]ページ先頭

©2009-2025 Movatter.jp