简体   繁体   English

找不到模块“./*.env”节点js

[英]Cannot find module './*.env' node js

when i try run npm run start o node server.js i get this error by require('./.env'), before run node server.js i run npm prune && npm install, i'm begginer with node js当我尝试运行 npm run start o node server.js 时,我通过 require('./.env') 收到此错误,在运行 node server.js 之前,我运行 npm prune && npm install,我开始使用 node js

module.js:478
throw err;


Error: Cannot find module './*.env'
    at Function.Module._resolveFilename (module.js:476:15)
    at Function.Module._load (module.js:424:25)
    at Module.require (module.js:504:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (C:\Users\luis.a.dominguez\Desktop\inovation\code\app-endpoints-service\server.js:3:11)
    at Module._compile (module.js:577:32)
    at Object.Module._extensions..js (module.js:586:10)
    at Module.load (module.js:494:32)
    at tryModuleLoad (module.js:453:12)
    at Function.Module._load (module.js:445:3)

I've noticed this happening several times for me when I used ngx-rocket to scaffold a new project.当我使用 ngx-rocket 搭建一个新项目时,我注意到这种情况发生了好几次。 For some reason it seemed to miss part of the script.出于某种原因,它似乎遗漏了部分脚本。

Anyways it was easily fixed by running this command at the same root level as your app:无论如何,通过在与您的应用程序相同的根级别运行此命令,可以轻松修复它:

npm run env npm 运行环境

Then build your app and run it, that should solve the problem.然后构建您的应用程序并运行它,这应该可以解决问题。

You can use dotenv it is easy to use and it can help you get the .env file with all the variables that are inside.您可以使用dotenv它很容易使用,它可以帮助您获取包含所有变量的.env文件。 It can be something like:它可以是这样的:

/yourfile.env /你的文件.env

NAME=YOUR_NAME

/index.js /index.js

require('dotenv').config({path: 'yourfile.env'}
console.log(process.env.NAME) //YOUR_NAME

With module js (.mjs) you do:使用模块 js (.mjs) 你可以:

Install dotenv module.安装 dotenv 模块。 Yarn: yarn dotenv or Npm: npm i dotenv纱线: yarn dotenvyarn dotenvnpm i dotenv

import dotenv from 'dotenv'
# all imports...
dotenv.config()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM