patch-1
@reggi/path-to-regexp
dependabot/npm_and_yarn/main/copy-to-clipboard-4.0.2
dependabot/npm_and_yarn/main/eslint-10.4.0
dependabot/npm_and_yarn/main/npmcli/eslint-config-7.0.0
dependabot/npm_and_yarn/main/proc-log-7.0.0
dependabot/npm_and_yarn/npm_and_yarn-826852524d
dependabot/npm_and_yarn/npm_and_yarn-ab9a7f4bc2
deprecate-totp-2fa
dhei/classic-tokens
gat-bypass-2fa-docs
jpg619/fix-accessibility-content-flow
jpg619/version-bump-tar-2
kartykp/gat-bypass-2fa-docs
kartykp/upgrade-path-to-regex
main
maitxn/version-bump-tar
patch-1
reggi/cache-based-on-version
reggi/dev-engines
reggi/fix-transform-prettier
reggi/overrides
update-search-sensitivity
| 1 | --- |
| 2 | title: Using npm packages in your projects |
| 3 | --- |
| 4 | |
| 5 | Once you have [installed a package][install-pkg] in `node_modules`, you can use it in your code. |
| 6 | |
| 7 | ## Using unscoped packages in your projects |
| 8 | |
| 9 | ### Node.js module |
| 10 | |
| 11 | 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. |
| 12 | |
| 13 | ```javascript |
| 14 | var lodash = require('lodash'); |
| 15 | |
| 16 | var output = lodash.without([1, 2, 3], 1); |
| 17 | console.log(output); |
| 18 | ``` |
| 19 | |
| 20 | ### package.json file |
| 21 | |
| 22 | In `package.json`, list the package under dependencies. You can optionally include a [semantic version][semver]. |
| 23 | |
| 24 | ```json |
| 25 | { |
| 26 | "dependencies": { |
| 27 | "package_name": "^1.0.0" |
| 28 | } |
| 29 | } |
| 30 | ``` |
| 31 | |
| 32 | ## Using scoped packages in your projects |
| 33 | |
| 34 | To use a scoped package, simply include the scope wherever you use the package name. |
| 35 | |
| 36 | ### Node.js module |
| 37 | |
| 38 | ```js |
| 39 | var projectName = require("@scope/package-name") |
| 40 | ``` |
| 41 | |
| 42 | ### package.json file |
| 43 | |
| 44 | In `package.json`: |
| 45 | |
| 46 | ```json |
| 47 | { |
| 48 | "dependencies": { |
| 49 | "@scope/package_name": "^1.0.0" |
| 50 | } |
| 51 | } |
| 52 | ``` |
| 53 | |
| 54 | ## Resolving "Cannot find module" errors |
| 55 | |
| 56 | 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: |
| 57 | |
| 58 | ``` |
| 59 | module.js:340 |
| 60 | throw err; |
| 61 | ^ |
| 62 | Error: Cannot find module 'lodash' |
| 63 | ``` |
| 64 | |
| 65 | - For scoped packages, run `npm install <@scope/package_name>` |
| 66 | - For unscoped packages, run `npm install <package_name>` |
| 67 | |
| 68 | [install-pkg]: downloading-and-installing-packages |
| 69 | [semver]: about-semantic-versioning |