问题描述
在加载 .env
文件时,将env值传递给cypress根文件夹中的 getToken.js
脚本引发找不到模块 dotenv错误。我已经安装了 npm install dotenv
。有人可以告诉我我在这里想念的吗?柏树根文件夹中提供 .env
文件。
While loading the .env
file to pass env values to the getToken.js
script in the cypress root folder throws Cannot find module 'dotenv'error. I have installed npm install dotenv
. Could someone please advise what I am missing here ? .env
file is available in cypress root folder.
环境:Windows 10> git bash /命令提示符
Environment : Windows 10 > git bash /command prompt
const puppeteer = require("puppeteer");
require('dotenv').config({path: '.env'})
const baseURL = process.env.CYPRESS_BASE_URL
const testsUser = process.env.CYPRESS_TESTS_USERNAME
puppeteer
.launch({ headless: true, chromeWebSecurity: false, args: ['--no-sandbox'] })
.then(async browser => {
const page = await browser.newPage();
await page.goto(`${baseURL}/login`);
await page.waitFor(2000);
await page.waitForSelector("input[name=username]");
await page.type("input[name=username]", testsUser , {
delay: 50
});
browser.close();
});
package.json
package.json
"scripts": {
"cy:run": "cypress run",
"get-token-main": "node getToken.js && mv tokenData.json cypress/fixtures",
"cy:open-qa": "npm run get-token-main && cypress open"
内部/模块/cjs/loader.js:797
抛出错误;
^
internal/modules/cjs/loader.js:797 throw err; ^
错误:找不到模块 dotenv
需要堆栈:
-/e2e/getToken.js
在Function.Module._resolveFilename(内部/模块/cjs/loader.js:794:15)
在Function.Module._load(内部/模块/cjs/loader.js:687:27)
在Module.require(内部/模块/cjs/loader.js:849:19)处
在require(内部/模块/cjs/helpers.js:74:18)在对象处
。 (/e2e/getToken.js:3:16)
在Module._compile(内部/模块/cjs/loader.js:956:30)
在Object.Module._extensions..js(内部/modules/cjs/loader.js:973:10)
在Module.load(内部/模块/cjs/loader.js:812:32)
在Function.Module._load(内部/模块/cjs/loader.js:724:14)
在Function.Module.runMain(内部/模块/cjs/loader.js:1025:10){
代码:'MODULE_NOT_FOUND',
requireStack:['/e2e/getToken.js']
Error: Cannot find module 'dotenv'Require stack:- /e2e/getToken.js at Function.Module._resolveFilename (internal/modules/cjs/loader.js:794:15) at Function.Module._load (internal/modules/cjs/loader.js:687:27) at Module.require (internal/modules/cjs/loader.js:849:19) at require (internal/modules/cjs/helpers.js:74:18) at Object. (/e2e/getToken.js:3:16) at Module._compile (internal/modules/cjs/loader.js:956:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10) at Module.load (internal/modules/cjs/loader.js:812:32) at Function.Module._load (internal/modules/cjs/loader.js:724:14) at Function.Module.runMain (internal/modules/cjs/loader.js:1025:10) { code: 'MODULE_NOT_FOUND', requireStack: [ '/e2e/getToken.js' ]
推荐答案
npm install dotenv
npm install dotenv
或
npm install --dotenv-extended
npm install --dotenv-extended
这篇关于运行脚本时,无法找到模块“ dotenv”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!